| Complex Numbers

Previous | GNU C Language Extensions | Next

ISO C99 supports complex floating data types, and as an extension GCC supports them in C89 mode, and supports complex integer data types which are not part of ISO C99. You can declare complex types using the keyword `_Complex`. As an extension, the older GNU keyword `__complex__` is also supported.
For example, `_Complex double x;` declares `x` as a variable whose real part and imaginary part are both of type `double`. `_Complex short int y;` declares `y` to have real and imaginary parts of type `short int`; this is not likely to be useful, but it shows that the set of complex types is complete.
To write a constant with a complex data type, use the suffix `i` or `j` (either one; they are equivalent). For example, `2.5fi` has type `_Complex float` and `3i` has type `_Complex int`. Such a constant always has a pure imaginary value, but you can form any complex value you like by adding one to a real constant. This is a GNU extension; once TIGCC supports this, you should include `<complex.h>` and use the macros `I` or `_Complex_I` instead.
To extract the real part of a complex-valued expression exp, write `__real__ exp`. Likewise, use `__imag__` to extract the imaginary part. This is a GNU extension; for values of floating type, you should use the ISO C99 functions `crealf`, `creal`, `creall`, `cimagf`, `cimag` and `cimagl`, declared in `<complex.h>` (not yet available in TIGCC) and also provided as built-in functions by GCC.
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.
GCC can allocate complex automatic variables in a noncontiguous fashion; it's even possible for the real part to be in a register while the imaginary part is on the stack (or vice-versa).