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

Viewed 104

In C99, it looks like the '~' operator on a _Complex performs a complex conjugate. 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. 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?

1 Answers

This is in fact a gcc extension, documented in 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.

If you compile with the -pedantic flag, you'll get a warning for this:

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));
Related