[Main]
Title=Complex Numbers
Index=__complex__, __imag__, __real__, _Complex

[Top]
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 <CODE>_Complex</CODE>.  As an extension, the older GNU
keyword <CODE>__complex__</CODE> is also supported.
<BR><BR>
For example, <CODE>_Complex&nbsp;double&nbsp;x;</CODE> declares <CODE>x</CODE> as a
variable whose real part and imaginary part are both of type
<CODE><A HREF="$$INFOLINK(keywords/double)">double</A></CODE>.  <CODE>_Complex&nbsp;short&nbsp;int&nbsp;y;</CODE> declares <CODE>y</CODE> to
have real and imaginary parts of type <CODE><A HREF="$$INFOLINK(keywords/short)">short</A>&nbsp;<A HREF="$$INFOLINK(keywords/int)">int</A></CODE>; this is not
likely to be useful, but it shows that the set of complex types is
complete.
<BR><BR>
To write a constant with a complex data type, use the suffix <CODE>i</CODE> or
<CODE>j</CODE> (either one; they are equivalent).  For example, <CODE>2.5fi</CODE>
has type <CODE>_Complex&nbsp;float</CODE> and <CODE>3i</CODE> has type
<CODE>_Complex&nbsp;int</CODE>.  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 <CODE>&lt;complex.h&gt;</CODE> and
use the macros <CODE>I</CODE> or <CODE>_Complex_I</CODE> instead.
<BR><BR>
To extract the real part of a complex-valued expression <I>exp</I>, write
<CODE>__real__&nbsp;<I>exp</I></CODE>.  Likewise, use <CODE>__imag__</CODE> to
extract the imaginary part.  This is a GNU extension; for values of
floating type, you should use the ISO C99 functions <CODE>crealf</CODE>,
<CODE>creal</CODE>, <CODE>creall</CODE>, <CODE>cimagf</CODE>, <CODE>cimag</CODE> and
<CODE>cimagl</CODE>, declared in <CODE>&lt;complex.h&gt;</CODE> (not yet available in TIGCC) and also provided as
built-in functions by GCC.
<BR><BR>
The operator <CODE>~</CODE> 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 <CODE>conjf</CODE>,
<CODE>conj</CODE> and <CODE>conjl</CODE>, declared in <CODE>&lt;complex.h&gt;</CODE> and also
provided as built-in functions by GCC.
<BR><BR>
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).
