Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Keywords and Statements

C _Complex and complex numbers

Reference for the C _Complex keyword and the complex macro: float, double and long double complex types, the I constant, and how to print real and imaginary parts.

_Complex is the keyword behind C's built-in complex number types. A complex number has a real part and an imaginary part, and each part is a floating-point value. Combine the keyword with a floating type to get float _Complex, double _Complex or long double _Complex.

You rarely write _Complex directly. Include <complex.h> and use the macro complex (which expands to _Complex) and the imaginary unit I. All the arithmetic operators work on complex values, and the header adds functions such as creal, cimag, cabs, conj and csqrt.

C
#include <complex.h>

double complex z = 3.0 + 4.0 * I;

Try it

C Creating and using complex numbers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C The square root of minus one

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
float _Complex   fz;
double _Complex  dz;
long double _Complex ldz;

/* with <complex.h> */
double complex z = x + y * I;
Part Description
_Complex Type specifier combined with float, double or long double. A complex type is laid out like an array of two real values (real part first).
complex Macro from <complex.h> that expands to _Complex.
I Macro for the imaginary unit, of type const float _Complex.
_Imaginary / imaginary Optional pure-imaginary types; many implementations, including GCC, do not provide them.
_Complex_I The underlying constant behind I.

Functions: creal, cimag, cabs, carg, conj, cproj, cexp, clog, cpow, csqrt and trigonometric forms, each with f and l suffix versions for float and long double. See complex.h.

Errors / undefined behavior: <complex.h> functions can set errno or raise floating-point exceptions on domain errors. Division by a complex zero follows IEEE rules for infinities and NaN, not a trap.

Usage notes

  • Arithmetic works naturally: +, -, *, / and unary minus accept complex operands, mixing with real values.
  • Comparison: only == and != are allowed. < and > have no meaning for complex numbers and do not compile.
  • I is a macro, so avoid naming a variable I once <complex.h> is included. It is also why many programs use i as the loop variable.
  • Print the parts separately. printf has no complex conversion; pass creal(z) and cimag(z). The %+.1f trick prints the sign of the imaginary part.
  • Precision follows the type: use crealf/cabsf for float complex and creall/cabsl for long double complex.
  • Optional on some compilers. C11 made complex support optional (__STDC_NO_COMPLEX__); MSVC does not implement the C99 syntax. GCC and Clang do.
  • C++ differs. C++ uses the class template std::complex, which is not binary compatible with the C types in the standard.
  • Added in C99.