ISO C99 supports complex floating data types, and as an extension GCCsupports them in C89 mode and in C++, and supports complex integer datatypes which are not part of ISO C99. You can declare complex typesusing the keyword _Complex
. As an extension, the older GNUkeyword __complex__
is also supported.
For example, `_Complex double x;' declares x
as avariable whose real part and imaginary part are both of typedouble
. `_Complex short int y;' declares y
tohave real and imaginary parts of type short int
; this is notlikely to be useful, but it shows that the set of complex types iscomplete.
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 imaginaryvalue, but you can form any complex value you like by adding one to areal constant. This is a GNU extension; if you have an ISO C99conforming C library (such as GNU libc), and want to construct complexconstants of floating type, you should include <complex.h>
anduse the macros I
or _Complex_I
instead.
To extract the real part of a complex-valued expression exp, write__real__
exp. Likewise, use __imag__
toextract the imaginary part. This is a GNU extension; for values offloating type, you should use the ISO C99 functions crealf
,creal
, creall
, cimagf
, cimag
andcimagl
, declared in <complex.h>
and also provided asbuilt-in functions by GCC.
The operator `~' performs complex conjugation when used on a valuewith a complex type. This is a GNU extension; for values offloating type, you should use the ISO C99 functions conjf
,conj
and conjl
, declared in <complex.h>
and alsoprovided as built-in functions by GCC.
GCC can allocate complex automatic variables in a noncontiguousfashion; it's even possible for the real part to be in a register whilethe imaginary part is on the stack (or vice-versa). Only the DWARF2debug info format can represent this, so use of DWARF2 is recommended. If you are using the stabs debug info format, GCC describes a noncontiguouscomplex variable as if it were two separate variables of noncomplex type. If the variable's actual name is foo
, the two fictitiousvariables are named foo$real
and foo$imag
. You canexamine and set these two fictitious variables with your debugger.