# Problems with #include<complex.h>

• C/++/#
Hi there. I have been trying to implement complex.h library to make some calculations in c++.Anyways I am not sure why mi compiler does not run my code at all.
C:
#include <iostream>
#include <complex.h>
#include <stdio.h>
using namespace std;
int main()
{
double complex z = CMPLX(0.0, -0.0);
cout<<" z = "<<creal(z)<<" + " <<cimag(z)<<endl;
return 0;
}
Codeblocks says:
Error: expected initializer before z
Error: z was not declared in this scope
I am really confused because even in cpp.com this procedure for imaginary numbers is given.

If you could help me with how may I use, I would be really grateful.
Thanks a lot

Last edited by a moderator:

mfb
Mentor
double complex z
I don't know where you got that from but that cannot work. You are trying to assign a double called "complex", and then the compiler has no idea what to do with the z.
Here are examples how to do it
complex<double> z

FactChecker
phyzguy
I think you are mixing C and C++ usage. The following compiles and runs in C:

C:
#include <complex.h>
#include <stdio.h>

int main()
{
double complex z = CMPLX(0.0, -0.0);
printf("z = %.1f%+.1fi\n", creal(z), cimag(z));
return 0;
}

The following code compiles and runs in C++:

Code:
#include <iostream>
#include <complex.h>
#include <stdio.h>
using namespace std;
int main()
{
complex<double> z(0.0, 0.0);
cout<<" z = "<<real(z)<<" + " <<imag(z)<<" i"<<endl;
return 0;
}

jim mcnamara, FactChecker, JorgeM and 1 other person