Contents|Index|Previous|Next

Complex Numbers

GNU C supports complex data types. You can declare both complex integer types and complex floating types, using the keyword, __complex__.

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.

To extract the real part of a complex-valued expression exp, write __real__exp. Likewise, use __imag__ to extract the imaginary part.

The tilde operator, (), performs complex conjugation when used on a value with a complex type.

GNU CC can allocate complex automatic variables in a noncontiguous fashion; its even possible for the real part to be in a register while the imaginary part is on the stack (or vice-versa). None of the supported debugging info formats has a way to represent noncontiguous allocation like this, so GNU CC describes a noncontiguous complex variable as if it were two separate variables of noncomplex type. If the variables actual name is foo, the two fictitious variables are named foo$real and foo$imag. You can examine and set these two fictitious variables with your debugger.

A future version of GDB will know how to recognize such pairs and treat them as a single variable with a complex type.