为什么 clang 说 _Imaginary_I 没有声明?

Why does clang say _Imaginary_I is not declared?

运行

clang test.c -o test

在这个文件上

#include <stdio.h>
#include <complex.h>

int main()
{
    _Complex double z = 1.0 + _Imaginary_I * 2.0;
    return 0;
}

由于

编译失败
error: use of undeclared identifier '_Imaginary_I'.

根据onlinepubs定义,_Imaginary_I。发生什么事了?

虚数和 _Imaginary_I 是 C 标准中的可选功能。

从 C11 开始,复数也是一个可选功能,但实现通常都支持它。 I_Complex_I 应该可以代替。


根据标准,您应该能够在编译时通过检查以下宏的值来测试一致性:

  • __STDC_IEC_559_COMPLEX__:值1表示存在复数和虚数类型,符合IEC 60559。
  • __STDC_NO_COMPLEX__:值1表示复数和虚数类型都不存在

然而实际上这并不可靠,例如gcc defines the macro without supporting the feature.