使用complex.h库在Visual Studio 2013中编译C代码

时间:2014-04-10 14:45:31

标签: c visual-studio visual-studio-2013 complex-numbers

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

C99支持添加了visual studio 2013,但我无法在我的“C”代码中使用complex.h。

#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;
}

我收到语法错误。

编辑:抱歉缺少部分。

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

2 个答案:

答案 0 :(得分:10)

如果有人在一年后搜索,请尝试

_Dcomplex dc1 = {3.0, 2.0};

用于变量声明。

从内部查看VS2013的“complex.h”标题,似乎微软决定自己实现C复数。你必须使用real()和imag()函数来实现自己的算术运算符,即:

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

答案 1 :(得分:1)

另一种方法是像这样定义:

/*_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));