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

12

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

Visual Studio 2013新增了对C99的支持,但我无法在我的"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

你在哪里遇到这些语法错误的? - Jabberwocky
7
如果您发布有关错误的问题,那么在问题中包括实际错误(完整的未编辑的)非常有帮助。请编辑您的问题以包含这些错误。 - Some programmer dude
5
似乎 complex 的支持并不像博客文章告诉我们的那样完整。 - Some programmer dude
非常不幸的是,VS对于更现代化的C版本支持得很差。 - CoffeeTableEspresso
2个回答

15

如果有人在一年后进行搜索,请尝试以下变量声明:

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

2

另一种方法是这样定义:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接