C complex.h Header

Beginner
6 min read
Updated: Nov 2026
3 examples
C99

What Is complex.h?

<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
UsePrefer
Create a complex valuedouble complex z = 1.0 + 2.0 * I;
Get the partscreal() / cimag()
Magnitude and anglecabs() / carg()
Other precisionsfloat complex, long double complex

How to Use complex.h in C

Include the header, declare a complex type, and read the parts with creal and cimag.

  1. Add #include <complex.h>.
  2. Declare values as double complex (or float complex / long double complex).
  3. Build a value with real + imag * I.
  4. Read the parts with creal(z) and cimag(z); never print a complex value with %f directly.
  5. Compile with -std=c99 or newer and add -lm for the c* functions.
PieceRequired?Role
#include <complex.h>YesDeclares complex, I, and the c* functions
double complexYesStores the real and imaginary parts
-lmUsuallyLinks 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

Cheat Sheet

Core names in complex.h. Each function has f (float) and l (long double) variants.

NameMeaning
complex / _ComplexType specifier for complex types
IThe 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, csqrtExponential, logarithm, power, square root
csin, ccos, ctanComplex trigonometry
crealf, creallfloat and long double variants

Worked Walkthrough — 3 + 4i

Split a complex number into parts, take its magnitude, and multiply two values.

Calculation
z = 3.0 + 4.0i

creal(z) = 3.0
cimag(z) = 4.0
cabs(z)  = sqrt(3*3 + 4*4) = sqrt(25) = 5.0
conj(z)  = 3.0 - 4.0i

(1 + 2i) * (3 - 1i)
  = 3 - 1i + 6i - 2i*i
  = 3 + 5i + 2
  = 5 + 5i

C Examples

Compile with gcc file.c -std=c11 -o out (add -lm at the end).

Example 1 — Read the Parts of a Complex Number

Create 3 + 4i and print its real part, imaginary part, and magnitude.

C
#include <complex.h>
#include <stdio.h>

int main(void) {
    double complex z = 3.0 + 4.0 * I;

    printf("real = %.1f\n", creal(z));
    printf("imag = %.1f\n", cimag(z));
    printf("|z|  = %.1f\n", cabs(z));
    return 0;
}

How It Works

  1. Build the value. 3.0 + 4.0 * I creates a double complex.
  2. Extract parts. creal and cimag return plain double values safe for printf.
  3. Take the magnitude. cabs computes √(3² + 4²) = 5.
  4. Compile with -lm. gcc complex_parts.c -std=c11 -o out -lm.

Example 2 — Add and Multiply Complex Numbers

Use the normal operators on complex values.

C
#include <complex.h>
#include <stdio.h>

int main(void) {
    double complex a = 1.0 + 2.0 * I;
    double complex b = 3.0 - 1.0 * I;
    double complex sum = a + b;
    double complex product = a * b;

    printf("sum     = %.1f + %.1fi\n", creal(sum), cimag(sum));
    printf("product = %.1f + %.1fi\n", creal(product), cimag(product));
    return 0;
}

How It Works

  1. Operators just work. + and * operate on both parts as complex arithmetic.
  2. Sum. (1 + 2i) + (3 − i) = 4 + i.
  3. Product. (1 + 2i)(3 − i) = 3 + 5i − 2i² = 5 + 5i, because i² = −1.
  4. Print each part. Both results have non-negative parts, so a plain + in the format is correct.

Example 3 — Conjugate and Square Root

Use conj() and csqrt(). The format %+.1f always prints a sign.

C
#include <complex.h>
#include <stdio.h>

int main(void) {
    double complex z = 3.0 + 4.0 * I;
    double complex c = conj(z);
    double complex r = csqrt(-4.0 + 0.0 * I);

    printf("conj(3+4i)   = %.1f%+.1fi\n", creal(c), cimag(c));
    printf("csqrt(-4+0i) = %.1f%+.1fi\n", creal(r), cimag(r));
    return 0;
}

How It Works

  1. Conjugate. conj flips the sign of the imaginary part: 3 + 4i becomes 3 − 4i.
  2. Square root of a negative. The real sqrt(-4) fails, but csqrt returns the principal root 2i.
  3. 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.

Performance & Portability Notes

ApproachCostBest for
+, -, *Cheap inline arithmeticGeneral complex math
/More work than multiply, with range handlingDivision only when needed
cabs, csqrt, cexpComparable to real counterparts plus extra branchesMagnitude, 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.

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.

Next: ctype.h

Test characters and change case with isalpha, isdigit, tolower, and toupper.

ctype.h tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful