Site Tools


h-complex

<complex.h>

<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.

Example

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;
}

Common functions

  • double complex: complex number type
  • creal(z), cimag(z): real and imaginary parts
  • cabs(z): absolute value (magnitude)
  • carg(z): phase angle
  • conj(z): complex conjugate
  • cexp(z), clog(z), cpow(z1, z2): exponential, log, power
  • csin(z), ccos(z), ctan(z): trigonometric
  • I: imaginary unit constant
h-complex.md · Last modified: by 127.0.0.1