_Complex 上的波浪号“~”运算符,它有什么作用?它是一个扩展吗?
Tilde '~' operator on _Complex, what does it do? Is it an extension?
在 C99 中,_Complex 上的“~”运算符似乎执行复数共轭。以下代码:
#include <complex.h>
#include <stdio.h>
int main()
{
double _Complex a = 2 + 3 * I;
printf("%f,%f\n", creal(~a), cimag(~a));
}
给出输出:
2.000000,-3.000000
这在 gcc 和 clang 中的行为相同。这是一个扩展吗?我似乎无法在各种标准文档中找到对它的任何引用google。
如果是扩展,有办法停用吗?
这实际上是一个 gcc 扩展,记录在 section 6.11 of the gcc manual:
The operator ~
performs complex conjugation when used on a value with a complex type. This is a GNU extension; for values of floating type, you should use the ISO C99 functions conjf
, conj
and conjl
, declared in <complex.h>
and also provided as built-in functions by GCC.
如果您使用 -pedantic
标志进行编译,您将收到一条警告:
x1.c: In function ‘main’:
x1.c:6:29: warning: ISO C does not support ‘~’ for complex conjugation [-Wpedantic]
printf("%f,%f\n", creal(~a), cimag(~a));
^
x1.c:6:40: warning: ISO C does not support ‘~’ for complex conjugation [-Wpedantic]
printf("%f,%f\n", creal(~a), cimag(~a));
在 C99 中,_Complex 上的“~”运算符似乎执行复数共轭。以下代码:
#include <complex.h>
#include <stdio.h>
int main()
{
double _Complex a = 2 + 3 * I;
printf("%f,%f\n", creal(~a), cimag(~a));
}
给出输出:
2.000000,-3.000000
这在 gcc 和 clang 中的行为相同。这是一个扩展吗?我似乎无法在各种标准文档中找到对它的任何引用google。
如果是扩展,有办法停用吗?
这实际上是一个 gcc 扩展,记录在 section 6.11 of the gcc manual:
The operator
~
performs complex conjugation when used on a value with a complex type. This is a GNU extension; for values of floating type, you should use the ISO C99 functionsconjf
,conj
andconjl
, declared in<complex.h>
and also provided as built-in functions by GCC.
如果您使用 -pedantic
标志进行编译,您将收到一条警告:
x1.c: In function ‘main’:
x1.c:6:29: warning: ISO C does not support ‘~’ for complex conjugation [-Wpedantic]
printf("%f,%f\n", creal(~a), cimag(~a));
^
x1.c:6:40: warning: ISO C does not support ‘~’ for complex conjugation [-Wpedantic]
printf("%f,%f\n", creal(~a), cimag(~a));