为什么 'int16_t complex' 不起作用?
Why 'int16_t complex' does not work?
为什么 int16_t complex
在 x86 和 x86_64 机器上 int16_t
不编译,是 short int
上的 typedef?以下是使用 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;
}
错误:
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 ? */
编译器命令行:
gcc-5 --std=c99 ./complex.c -o ./complex
gcc-4.9 --std=c99 ./complex.c -o ./complex
只是因为没有int16_t complex
这样的类型。 C11 中有效的复杂类型是 float complex
、double complex
和 long double complex
。
complex
是一个宏 which expands to _Complex
,它是实际的类型说明符。
这是因为 _Complex
是类型名称本身的一部分,而不是可以与其他类型一起使用的修饰符。它不适用于任何类型的 typedef,不仅 short
。例如,如果你这样做
typedef float fp32;
fp32 _Complex x = I + I / 3 * I / 2;
您将看到相同的编译错误。
首先,复杂整数类型是 GCC 扩展。 C 标准说 (C11 6.2.5p11):
11 There are three complex types, designated as float _Complex
, double _Complex
, and long double _Complex
.43) (Complex types are a conditional feature that implementations need not support; see 6.10.8.3.) The real floating and complex types are collectively called the floating types.
_Complex
是类型名称的一部分,就像 long
一样。你也不能这样做:
typedef double dbl;
typedef long dbl ldbl;
即尝试为 ldbl
为 long double
定义 typedef
当使用 typedef 为双!同样,在定义复杂类型时不能使用typedef
(例如int16_t
),而是使用short int _Complex
。
(这当然也适用于 complex
,因为它只是一个扩展为 _Complex
的宏)。
为什么 int16_t complex
在 x86 和 x86_64 机器上 int16_t
不编译,是 short int
上的 typedef?以下是使用 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;
}
错误:
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 ? */
编译器命令行:
gcc-5 --std=c99 ./complex.c -o ./complex
gcc-4.9 --std=c99 ./complex.c -o ./complex
只是因为没有int16_t complex
这样的类型。 C11 中有效的复杂类型是 float complex
、double complex
和 long double complex
。
complex
是一个宏 which expands to _Complex
,它是实际的类型说明符。
这是因为 _Complex
是类型名称本身的一部分,而不是可以与其他类型一起使用的修饰符。它不适用于任何类型的 typedef,不仅 short
。例如,如果你这样做
typedef float fp32;
fp32 _Complex x = I + I / 3 * I / 2;
您将看到相同的编译错误。
首先,复杂整数类型是 GCC 扩展。 C 标准说 (C11 6.2.5p11):
11 There are three complex types, designated as
float _Complex
,double _Complex
, andlong double _Complex
.43) (Complex types are a conditional feature that implementations need not support; see 6.10.8.3.) The real floating and complex types are collectively called the floating types.
_Complex
是类型名称的一部分,就像 long
一样。你也不能这样做:
typedef double dbl;
typedef long dbl ldbl;
即尝试为 ldbl
为 long double
定义 typedef
当使用 typedef 为双!同样,在定义复杂类型时不能使用typedef
(例如int16_t
),而是使用short int _Complex
。
(这当然也适用于 complex
,因为它只是一个扩展为 _Complex
的宏)。