1

The following test.c can be compiled successfully by using gcc compiler:

$ cat test.c
#include <complex.h>

int main(void) {
    double complex a = 0;
    return 0;
}
$ gcc -o test1 test.c
$

While after changing the name to test.cpp, and compile it with g++ compiler, there is an error:

$ cat test.cpp
#include <complex.h>

int main(void) {
    double complex a = 0;
    return 0;
}

$ g++ -o test2 test.cpp
test.cpp: In function ‘int main()’:
test.cpp:4:20: error: expected initializer before ‘a’
     double complex a = 0;
                    ^

Why can the same code be compiled successfully through gcc while not g++?

P.S. My compiler versions:

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --disable-multilib --disable-werror --enable-checking=release
Thread model: posix
gcc version 6.2.1 20160830 (GCC)
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/6.2.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --disable-multilib --disable-werror --enable-checking=release
Thread model: posix
gcc version 6.2.1 20160830 (GCC)
Nan Xiao
  • 16,671
  • 18
  • 103
  • 164
  • 1
    In C "Complex types are a conditional feature that implementations need not support; see 6.10.8.3.) ", perhaps the C++ compiler lacks `complex` support or some compile option needed? – chux - Reinstate Monica Jan 03 '17 at 05:27
  • @perreal: Still the same error. – Nan Xiao Jan 03 '17 at 05:29
  • C++ has a complex type built in to the std class. The g++ compiler thinks you are using that one, which you would declare like this: `std::complex my_complex(1,1);` – bruceg Jan 03 '17 at 05:30
  • 2
    _The headers , , , and do not contain any content from the C standard library and instead merely include other headers from the C++ standard library. The use of all these headers is deprecated in C++._ – Danh Jan 03 '17 at 05:30
  • @NanXiao The way you declared complex type is incorrect.. – Praburaj Jan 03 '17 at 05:32
  • 3
    This is another example for C is not a subset of C++. Don't expect anything works with C will work with C++ – Danh Jan 03 '17 at 05:36
  • 2
    Because C is not a subset if C++. – n. m. could be an AI Jan 03 '17 at 05:38

3 Answers3

3
$ cat test.cpp
#include <complex>

int main(void) {
    std::complex<double> a = 0;
    return 0;
}

In C++, complex is a template, here is the reference std::complex.

lxyscls
  • 297
  • 1
  • 3
  • 17
3

The basic reason is that C and C++ support complex numbers in fundamentally different ways, which are code incompatible. So C code which uses complex variables, and operations on them, cannot be used in any version of C++, and vice versa.

In C++ (all versions) complex arithmetic is supported by a templated class named complex, which is defined in header <complex>, and is in namespace std. Since it is a C++ (templated) class in namespace std, which C does not support at all, this type and the C++ header <complex> cannot be used in C.

Standard C, before 1999, did not support complex variables or arithmetic. In C (since 1999), complex arithmetic is supported using a _Complex keyword, and a convenience macro complex (#defined in <complex.h>) which uses it. C++ does not support the _Complex keyword or <complex.h>.

Peter
  • 35,646
  • 4
  • 32
  • 74
  • Note that g++ does support `_Complex` for compatibility with C, if you rename `complex` to `_Complex` the example compiles. However, the macro `complex` is disabled because it would break valid C++ code. – Marc Glisse Jan 03 '17 at 11:28
1

For C++ Complex type : http://en.cppreference.com/w/cpp/numeric/complex

For C Complex type: How to work with complex numbers in C?

The Complex.h works differently in C and C++

Community
  • 1
  • 1
Praburaj
  • 613
  • 8
  • 21