<complex.h> is the C99 header for complex numbers. It defines the complex macro (a spelling of _Complex), the imaginary unit I, and functions such as creal, cimag, cabs, conj, and csqrt.
A double complex value stores a real part and an imaginary part as two doubles. The ordinary operators + - * / work on complex values directly; the library adds the math functions around them.
Remember
#include <complex.h>
double complex z = 3.0 + 4.0 * I;
creal(z) -> real part (3.0)
cimag(z) -> imaginary part (4.0)
cabs(z) -> magnitude (5.0)
conj(z) -> 3.0 - 4.0i
link with -lm on GCC and Clang
Use
Prefer
Create a complex value
double complex z = 1.0 + 2.0 * I;
Get the parts
creal() / cimag()
Magnitude and angle
cabs() / carg()
Other precisions
float complex, long double complex
Approach
How to Use complex.h in C
Include the header, declare a complex type, and read the parts with creal and cimag.
Add #include <complex.h>.
Declare values as double complex (or float complex / long double complex).
Build a value with real + imag * I.
Read the parts with creal(z) and cimag(z); never print a complex value with %f directly.
Compile with -std=c99 or newer and add -lm for the c* functions.
Piece
Required?
Role
#include <complex.h>
Yes
Declares complex, I, and the c* functions
double complex
Yes
Stores the real and imaginary parts
-lm
Usually
Links the math library on GCC and Clang
Pseudocode
Pseudocode
z = real + imag * I
a = creal(z) // real part
b = cimag(z) // imaginary part
m = cabs(z) // sqrt(a*a + b*b)
c = conj(z) // real - imag * I
r = csqrt(z) // principal square root
Reference
Cheat Sheet
Core names in complex.h. Each function has f (float) and l (long double) variants.
Name
Meaning
complex / _Complex
Type specifier for complex types
I
The imaginary unit (_Complex_I)
creal(z), cimag(z)
Real and imaginary parts
cabs(z)
Magnitude |z|
carg(z)
Phase angle in radians
conj(z)
Complex conjugate
cexp, clog, cpow, csqrt
Exponential, logarithm, power, square root
csin, ccos, ctan
Complex trigonometry
crealf, creall
float and long double variants
Trace
Worked Walkthrough — 3 + 4i
Split a complex number into parts, take its magnitude, and multiply two values.
Conjugate.conj flips the sign of the imaginary part: 3 + 4i becomes 3 − 4i.
Square root of a negative. The real sqrt(-4) fails, but csqrt returns the principal root 2i.
Signed formatting.%+.1f prints -4.0 or +2.0 so the result reads like a math expression.
Edge Cases & Pitfalls
These mistakes cause wrong output or build failures.
printf
Print parts, not the value
printf("%f", z) is undefined. Print creal(z) and cimag(z) separately.
names
I and complex are macros
Including the header reserves them. Do not name a variable I or complex in the same file.
prec
Match precision
Use csqrtf for float complex and csqrtl for long double complex. Mixing types converts silently.
msvc
MSVC differs
MSVC does not implement C99 _Complex; it offers its own _Dcomplex structure. Use GCC or Clang for portable examples.
Analysis
Performance & Portability Notes
Approach
Cost
Best for
+, -, *
Cheap inline arithmetic
General complex math
/
More work than multiply, with range handling
Division only when needed
cabs, csqrt, cexp
Comparable to real counterparts plus extra branches
Magnitude, roots, exponentials
Complex support is optional in C11 hosted implementations: check that __STDC_NO_COMPLEX__ is not defined. Prefer float complex for large arrays when single precision is enough.
Remember
Key Takeaways
Role: complex numbers with double complex and I.
Parts: read with creal and cimag; never print a complex value directly.
Link: add -lm on GCC and Clang for the c* functions.
Portability: MSVC does not support C99 _Complex.
One line:complex.h gives C native complex arithmetic—build values with I, read them with creal/cimag, and use the c* functions for the math.
Frequently Asked Questions
complex.h is the C99 header for complex numbers. It defines the complex type macro, the imaginary unit I, and functions such as creal, cimag, cabs, conj, and csqrt.
Declare a double complex variable and combine a real part with the imaginary unit: double complex z = 3.0 + 4.0 * I;
Print its parts separately: printf("%.1f%+.1fi", creal(z), cimag(z)). There is no printf conversion for a whole complex value.
Usually yes on GCC and Clang for functions such as cabs, csqrt, and cexp. Basic arithmetic with + - * / does not need it.
It is part of C99 and optional in C11 (check __STDC_NO_COMPLEX__). MSVC does not implement C99 _Complex.
tgmath.h adds type-generic macros so sqrt(z) automatically calls csqrt for a complex argument and sqrt for a double.