C Reference ยท ctype.h, math.h and others
C complex.h: complex number arithmetic
Reference for complex.h: double complex, the I constant, CMPLX, creal, cimag, cabs, carg, conj, csqrt, cexp and common pitfalls, with runnable examples.
C has built-in complex types: float complex, double complex and long double complex. Including <complex.h> gives you the macro complex (a nicer spelling of the keyword _Complex), the imaginary unit I, and a family of functions to take apart and transform complex numbers. The operators +, -, * and / work on complex values directly.
There is no printf conversion for complex numbers, so you print the real and imaginary parts separately with creal() and cimag().
#include <complex.h> double complex z = 3.0 + 4.0 * I; double length = cabs(z); /* 5.0 */
Try it
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.
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
double creal(double complex z); double cimag(double complex z); double cabs(double complex z); double carg(double complex z); double complex conj(double complex z); double complex cproj(double complex z); double complex CMPLX(double x, double y); /* C11 macro */ double complex cexp(double complex z); double complex clog(double complex z); double complex cpow(double complex x, double complex y); double complex csqrt(double complex z); double complex csin(double complex z); double complex ccos(double complex z); double complex ctan(double complex z);
| Name | Description |
|---|---|
creal, cimag |
The real part and the imaginary part (as real numbers). |
cabs |
Magnitude (the modulus), like hypot(re, im). |
carg |
Argument (the phase angle) in radians, in the range [-pi, pi]. |
conj |
Complex conjugate: the sign of the imaginary part is flipped. |
cproj |
Projection onto the Riemann sphere; only matters for infinities. |
CMPLX(x, y) |
Builds a complex number from two reals without arithmetic, so no NaN or sign-of-zero surprises. CMPLXF and CMPLXL exist for float and long double. |
cexp, clog, cpow, csqrt |
Complex exponential, natural logarithm (principal value), power and square root (principal root, real part never negative). |
csin, ccos, ctan, casin, cacos, catan, csinh, ccosh, ctanh, casinh, cacosh, catanh |
Trigonometric and hyperbolic functions and their inverses. |
Every function has a float version with an f suffix (cabsf) and a long double version with an l suffix (cabsl). The macros complex, I and _Complex_I come from the header.
Returns: the mathematical result, or a value with NaN or infinite parts for singular inputs. Branch cuts follow the usual principal-value conventions, so negative real inputs to clog and csqrt approach the cut from the side given by the sign of the imaginary zero.
Usage notes
- Only
==and!=compare complex values.<and>are errors. Compare magnitudes withcabs()when you need an ordering. - Converting complex to real throws away the imaginary part silently:
double x = z;keepscreal(z). Callcreal()explicitly to show intent. 3.0 + 4.0 * Iis an expression, not a literal.Iis aconst float _Complex, soINFINITY * Iproduces NaN in the real part.CMPLX(0.0, INFINITY)is exact.csqrtof a negative real needs a complex argument.sqrt(-9.0)is NaN;csqrt(-9.0)accepts the real, butCMPLX(-9.0, 0.0)makes the intent and the zero's sign explicit.- Prefer
cabs(z)tosqrt(re*re + im*im). It avoids overflow and underflow in the intermediate squares, the wayhypotdoes. - Floating-point dust:
cexp(I * pi)is-1with an imaginary part near1.2e-16. Test with a tolerance, never== -1. - Complex support is optional since C11. Implementations that do not provide it define
__STDC_NO_COMPLEX__. MSVC's<complex.h>uses its own_Dcomplexstructs and a different API. - Imaginary types (
_Imaginary,_Imaginary_I) are optional and not available in GCC. - Plain names: with
<tgmath.h>you writesqrt,expandfabsand the compiler picks the complex version for you. - Standard version: C99;
CMPLXis C11.