sor*_*h-r 3 c gcc c99 complex-numbers c11
为什么int16_t complex不在int16_tx86和x86_64机器上编译,是否为typedef short int?以下是使用gcc 5.4和4.9测试的示例代码,其中包含C99和C11标准.编译器抱怨在声明说明符中有两个或更多数据类型.
码:
#include <complex.h>
#include <stdint.h>
#include <stdio.h>
int main()
{
float complex x = I + I / 3 * I / 2;
short int complex y = I + I / 3 * I / 2;
int16_t complex z = I + I / 3 * I / 2; /* Why ? */
printf("x=(%+f,%+f)\n", creal(x), cimag(x));
printf("y=(%+f,%+f)\n", creal(y), cimag(y));
printf("z=(%+f,%+f)\n", creal(z), cimag(z)); /* Why ? */
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误:
In file included from ./complex.c:1:0:
./complex.c: In function ‘main’:
./complex.c:9:13: error: two or more data types in declaration specifiers
int16_t complex z = I + I / 3 * I / 2; /* Why ? */
Run Code Online (Sandbox Code Playgroud)
编译器命令行:
gcc-5 --std=c99 ./complex.c -o ./complex
gcc-4.9 --std=c99 ./complex.c -o ./complex
Run Code Online (Sandbox Code Playgroud)
首先,复杂的整数类型是GCC扩展.C标准说(C11 6.2.5p11):
11有三种复杂类型,指定为
float _Complex,double _Complex和long double _Complex.43)(复杂类型是实现不需要支持的条件特征;请参阅6.10.8.3.)实际的浮动和复杂类型统称为浮动类型.
这_Complex是类型名称的一部分,就像long.你不能这样做:
typedef double dbl;
typedef long dbl ldbl;
Run Code Online (Sandbox Code Playgroud)
即试图定义一个typedef用于ldbl对long double使用一个typedef双时!同样,在定义复杂类型时不能使用typedef(例如int16_tis),short int _Complex而是使用.
(当然这也适用于complex,因为它只是一个扩展到的宏_Complex).