<complex.h> provides _Complex type (C99) and complex number functions: magnitude (cabs), phase (carg), conjugate (conj), and full trigonometric/exponential support. Arithmetic operators work natively on complex types.
Use it for signal processing, electrical engineering, and quantum mechanics where complex arithmetic is essential.
This example performs complex arithmetic and demonstrates Euler's formula relationship.
// compile: gcc -std=c99 -o complexexample complexexample.c -lm // run: ./complexexample // description: complex arithmetic and magnitude calculation #include <complex.h> #include <math.h> #include <stdio.h> int main() { double complex z = 3.0 + 4.0 * I; printf("|z| = %.1f\n", cabs(z)); printf("arg(z) = %.4f rad\n", carg(z)); printf("conj(z) = %.1f%+.1fi\n", creal(conj(z)), cimag(conj(z))); double complex euler = cexp(I * M_PI); printf("e^(i*pi) ≈ %.6f%+.6fi\n", creal(euler), cimag(euler)); return 0; }
double complex: complex number typecreal(z), cimag(z): real and imaginary partscabs(z): absolute value (magnitude)carg(z): phase angleconj(z): complex conjugatecexp(z), clog(z), cpow(z1, z2): exponential, log, powercsin(z), ccos(z), ctan(z): trigonometricI: imaginary unit constant