Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

<complex.h> — complex arithmetic

The <complex.h> header provides macros and functions for complex numbers: constructing values and reading their real and imaginary components.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <complex.h>
double complex z = CMPLX(real_part, imaginary_part);

<complex.h> supports complex arithmetic. The complex macro expands to _Complex; types include float complex, double complex, and long double complex. The I macro represents the imaginary unit, whose square is −1.

CMPLX(x, y) constructs a double complex with real component x and imaginary component y. Functions creal and cimag read these components, while cabs computes magnitude. In function families, suffixes f and l select the float and long double variants.

Support is optional: an implementation defining __STDC_NO_COMPLEX__ need not provide the header. A repository C23 flag does not attest production runtime support.

Examples

#if defined(__STDC_NO_COMPLEX__)
#error Complex arithmetic is required
#else
#include <complex.h>
#include <stdio.h>

int main(void)
{
    double complex z = CMPLX(3.0, 4.0);
    printf("real=%.0f imag=%.0f\n", creal(z), cimag(z));
    return 0;
}
#endif

Result

real=3 imag=4

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources