Category: 📂 std/ (C Standard Library)
Header: <complex.h>
Scope: C99
The complex.h header declares functions and macros for performing arithmetic on complex numbers.
| Facility Category | Key Symbols | Description |
|---|---|---|
| Arithmetic | creal, cimag, cabs, carg, conj | Extracting parts and modifying complex values. |
| Functions | cexp, clog, csqrt, csin, ccos | Complex versions of standard math functions. |
| Types & Macros | double complex, I | Native complex types and imaginary unit. |
Native keyword for complex types.
#define I _Complex_IThe imaginary unit i.
double creal(double complex z)Extracts the real component.
double cimag(double complex z)Extracts the imaginary component.
Example
#include <complex.h>
#include <stdio.h>
int main(void) {
double complex z = 3.0 + 4.0 * I;
printf("Imaginary: %.1f\n", cimag(z)); // 4.0
return 0;
}double cabs(double complex z)Example
#include <complex.h>
#include <stdio.h>
int main(void) {
double complex z = 3.0 + 4.0 * I;
printf("Magnitude: %.1f\n", cabs(z));
return 0;
}double carg(double complex z)Computes the argument (phase angle) of z.
Example
#include <complex.h>
#include <stdio.h>
int main(void) {
double complex z = 1.0 + 1.0 * I;
printf("Argument: %.3f\n", carg(z)); // ≈ 0.785 (π/4)
return 0;
}double complex conj(double complex z)Computes the complex conjugate of z.
Example
#include <complex.h>
#include <stdio.h>
int main(void) {
double complex z = 3.0 + 4.0 * I;
double complex c = conj(z);
printf("Conjugate: %.1f + %.1fi\n", creal(c), cimag(c)); // 3.0 - 4.0i
return 0;
}double complex cexp(double complex z)Computes the complex exponential of z.
Example
#include <complex.h>
int main(void) {
double complex z = 1.0 + 0.0 * I;
double complex e = cexp(z); // ≈ 2.718 + 0i
return 0;
}double complex clog(double complex z)Computes the complex natural logarithm of z.
Example
#include <complex.h>
int main(void) {
double complex z = 1.0 + 0.0 * I;
double complex l = clog(z); // ≈ 0 + 0i
return 0;
}double complex csqrt(double complex z)Computes the complex square root of z.
Example
#include <complex.h>
int main(void) {
double complex z = -1.0 + 0.0 * I;
double complex s = csqrt(z); // ≈ 0 + 1i
return 0;
}double complex csin(double complex z)Computes the complex sine of z.
Example
#include <complex.h>
int main(void) {
double complex z = 0.0 + 0.0 * I;
double complex s = csin(z); // 0 + 0i
return 0;
}double complex ccos(double complex z)Computes the complex cosine of z.
Example
#include <complex.h>
int main(void) {
double complex z = 0.0 + 0.0 * I;
double complex c = ccos(z); // 1 + 0i
return 0;
}double complex ctan(double complex z)Computes the complex tangent of z.
Example
#include <complex.h>
int main(void) {
double complex z = 0.0 + 0.0 * I;
double complex t = ctan(z); // 0 + 0i
return 0;
}