简体   繁体   English

_Complex 上的波浪号“~”运算符,它有什么作用? 它是一个扩展吗?

[英]Tilde '~' operator on _Complex, what does it do? Is it an extension?

In C99, it looks like the '~' operator on a _Complex performs a complex conjugate.在 C99 中,_Complex 上的“~”运算符看起来像是执行复共轭。 The following code:以下代码:

#include <complex.h>
#include <stdio.h>
int main()
{
    double _Complex a = 2 + 3 * I;
    printf("%f,%f\n", creal(~a), cimag(~a));
}

Gives the output:给出输出:

2.000000,-3.000000

This behaves the same in both gcc and clang.这在 gcc 和 clang 中的行为相同。 Is this an extension?这是一个扩展吗? I can't seem to find any reference to it in the various standards documents google pulled up.我似乎无法在谷歌提取的各种标准文档中找到任何对它的引用。

If it is an extension, is there a way to deactivate it?如果是扩展程序,有没有办法停用它?

This is in fact a gcc extension, documented in section 6.11 of the gcc manual :这实际上是一个 gcc 扩展,记录在gcc 手册的第 6.11 节中

The operator ~ performs complex conjugation when used on a value with a complex type.运算符~在用于具有复杂类型的值时执行复杂的共轭。 This is a GNU extension;这是一个 GNU 扩展; 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.对于浮点类型的值,您应该使用 ISO C99 函数conjfconjconjl ,它们在<complex.h>声明并且也由 GCC 作为内置函数提供。

If you compile with the -pedantic flag, you'll get a warning for this:如果您使用-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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM