Lebannen
Lebannen

Reputation: 164

Compiling C code in Visual Studio 2013 with complex.h library

http://blogs.msdn.com/b/vcblog/archive/2013/07/19/c99-library-support-in-visual-studio-2013.aspx

C99 support added visual studio 2013, but I cant use complex.h in my "C" code.

#include <stdio.h>
#include <complex.h>
int main(void)
{
    double complex dc1 = 3 + 2 * I;
    double complex dc2 = 4 + 5 * I;
    double complex result;

    result = dc1 + dc2;
    printf(" ??? \n", result);

    return 0;
}

I get syntax errors.

Edit: Sorry for the missing part.

error C2146: syntax error : missing ';' before identifier 'dc1'
error C2065: 'dc1' : undeclared identifier
error C2088: '*' : illegal for struct
error C2086: 'double complex' : redefinition
error C2146: syntax error : missing ';' before identifier 'dc2'
error C2065: 'dc2' : undeclared identifier
error C2088: '*' : illegal for struct
error C2086: 'double complex' : redefinition
error C2146: syntax error : missing ';' before identifier 'result'
error C2065: 'result' : undeclared identifier
error C2065: 'result' : undeclared identifier
error C2065: 'dc1' : undeclared identifier
error C2065: 'dc2' : undeclared identifier
error C2065: 'result' : undeclared identifier           
IntelliSense: expected a ';'
IntelliSense: expected a ';'
IntelliSense: expected a ';'
IntelliSense: identifier "result" is undefined
IntelliSense: identifier "dc1" is undefined
IntelliSense: identifier "dc2" is undefined

Upvotes: 13

Views: 6409

Answers (2)

Mohamad Sami
Mohamad Sami

Reputation: 21

Another way is to define like:

/*_Fcomplex */  _C_float_complex a =  _FCbuild(5.0F, 1.0F);    
printf( "z = %.1f% + .1fi\n", crealf(a), cimagf(a));    

/*_Dcomplex*/ _C_double_complex b = _Cbuild(3.0, 2.0);    
printf("z = %.1f% + .1fi\n",creal(b), cimag(b));    

Upvotes: 2

Tanaya
Tanaya

Reputation: 396

In case anyone is searching a year later, try

_Dcomplex dc1 = {3.0, 2.0};

for the variable declaration.

From looking inside VS2013's "complex.h" header, it seems that Microsoft decided on their own implementation for C complex numbers. You'll have to implement your own arithmetical operators using the real() and imag() functions, i.e.:

double real_part = real(dc1) + real(dc2);
double imag_part = imag(dc1) + imag(dc2);
_Dcomplex result = {real_part, imag_part};

Upvotes: 15

Related Questions