# Complex number arithmetic

> C++ Atlas: `cppatlas.com/w/c/numeric/complex` | Source: https://en.cppreference.com/w/c/numeric/complex | Domain: Numerics | Standards: C99, C11, C23

The C programming language, as of C99, supports complex number math with the three built-in types double _Complex, float _Complex, and long double _Complex (see _Complex).

## Official Specification Summary (cppreference)

If the macro constant `__STDC_NO_COMPLEX__` is defined by the implementation, the complex types, the header `<complex.h>` and all of the names listed here are not provided. (since C11)

The C programming language, as of C99, supports complex number math with the three built-in types `double _Complex`, `float _Complex`, and `long double _Complex` (see `_Complex`). When the header `<complex.h>` is included, the three complex number types are also accessible as `double complex`, `float complex`, `long double complex`.

In addition to the complex types, the three imaginary types may be supported: `double _Imaginary`, `float _Imaginary`, and `long double _Imaginary` (see `_Imaginary`). When the header `<complex.h>` is included, the three imaginary types are also accessible as `double imaginary`, `float imaginary`, and `long double imaginary`.

Standard arithmetic operators `+`, `-`, `*`, `/` can be used with real, complex, and imaginary types in any combination.

A compiler that defines `__STDC_IEC_559_COMPLEX__` is recommended, but not required to support imaginary numbers. POSIX recommends checking if the macro `_Imaginary_I` is defined to identify imaginary number support. (since C99) (until C11)

Imaginary numbers are supported if `__STDC_IEC_559_COMPLEX__` or `__STDC_IEC_60559_COMPLEX__` (since C23) is defined. (since C11)

Defined in header `<complex.h>`

**Types**

- `imaginary` (C99): imaginary type macro (keyword macro)
- `complex` (C99): complex type macro (keyword macro)

**The imaginary constant**

- `_Imaginary_I` (C99): the imaginary unit constant i (macro constant)
- `_Complex_I` (C99): the complex unit constant i (macro constant)
- `I` (C99): the complex or imaginary unit constant i (macro constant)

**Manipulation**

- `CMPLX` (C11), `CMPLXF` (C11), `CMPLXL` (C11): constructs a complex number from real and imaginary parts (function macro)
- `creal` (C99), `crealf` (C99), `creall` (C99): computes the real part of a complex number (function)
- `cimag` (C99), `cimagf` (C99), `cimagl` (C99): computes the imaginary part a complex number (function)
- `cabs` (C99), `cabsf` (C99), `cabsl` (C99): computes the magnitude of a complex number (function)
- `carg` (C99), `cargf` (C99), `cargl` (C99): computes the phase angle of a complex number (function)
- `conj` (C99), `conjf` (C99), `conjl` (C99): computes the complex conjugate (function)
- `cproj` (C99), `cprojf` (C99), `cprojl` (C99): computes the projection on Riemann sphere (function)

**Exponential functions**

- `cexp` (C99), `cexpf` (C99), `cexpl` (C99): computes the complex base-e exponential (function)
- `clog` (C99), `clogf` (C99), `clogl` (C99): computes the complex natural logarithm (function)

**Power functions**

- `cpow` (C99), `cpowf` (C99), `cpowl` (C99): computes the complex power function (function)
- `csqrt` (C99), `csqrtf` (C99), `csqrtl` (C99): computes the complex square root (function)

**Trigonometric functions**

- `csin` (C99), `csinf` (C99), `csinl` (C99): computes the complex sine (function)
- `ccos` (C99), `ccosf` (C99), `ccosl` (C99): computes the complex cosine (function)
- `ctan` (C99), `ctanf` (C99), `ctanl` (C99): computes the complex tangent (function)
- `casin` (C99), `casinf` (C99), `casinl` (C99): computes the complex arc sine (function)
- `cacos` (C99), `cacosf` (C99), `cacosl` (C99): computes the complex arc cosine (function)
- `catan` (C99), `catanf` (C99), `catanl` (C99): computes the complex arc tangent (function)

**Hyperbolic functions**

- `csinh` (C99), `csinhf` (C99), `csinhl` (C99): computes the complex hyperbolic sine (function)
- `ccosh` (C99), `ccoshf` (C99), `ccoshl` (C99): computes the complex hyperbolic cosine (function)
- `ctanh` (C99), `ctanhf` (C99), `ctanhl` (C99): computes the complex hyperbolic tangent (function)
- `casinh` (C99), `casinhf` (C99), `casinhl` (C99): computes the complex arc hyperbolic sine (function)
- `cacosh` (C99), `cacoshf` (C99), `cacoshl` (C99): computes the complex arc hyperbolic cosine (function)
- `catanh` (C99), `catanhf` (C99), `catanhl` (C99): computes the complex arc hyperbolic tangent (function)

### Notes

The following function names are potentially (since C23) reserved for future addition to `<complex.h>` and are not available for use in the programs that include that header: `cerf`, `cerfc`, `cexp2`, `cexpm1`, `clog10`, `clog1p`, `clog2`, `clgamma`, `ctgamma`, `csinpi`, `ccospi`, `ctanpi`, `casinpi`, `cacospi`, `catanpi`, `ccompoundn`, `cpown`, `cpowr`, `crootn`, `crsqrt`, `cexp10m1`, `cexp10`, `cexp2m1`, `clog10p1`, `clog2p1`, `clogp1` (since C23), along with their -`f` and -`l` suffixed variants.

Although the C standard names the inverse hyperbolic with "complex arc hyperbolic sine" etc., the inverse functions of the hyperbolic functions are the area functions. Their argument is the area of a hyperbolic sector, not an arc. The correct names are "complex inverse hyperbolic sine" etc. Some authors use "complex area hyperbolic sine" etc.

A complex or imaginary number is infinite if one of its parts is infinite, even if the other part is NaN.

A complex or imaginary number is finite if both parts are neither infinities nor NaNs.

A complex or imaginary number is a zero if both parts are positive or negative zeroes.

While MSVC does provide a `<complex.h>` header, it does not implement complex numbers as native types, but as `struct`s, which are incompatible with standard C complex types and do not support the `+`, `-`, `*`, `/` operators.

### Example

```c
#include <complex.h>
#include <stdio.h>
#include <tgmath.h>

int main(void)
{
    double complex z1 = I * I;     // imaginary unit squared
    printf("I * I = %.1f%+.1fi\n", creal(z1), cimag(z1));

    double complex z2 = pow(I, 2); // imaginary unit squared
    printf("pow(I, 2) = %.1f%+.1fi\n", creal(z2), cimag(z2));

    double PI = acos(-1);
    double complex z3 = exp(I * PI); // Euler's formula
    printf("exp(I*PI) = %.1f%+.1fi\n", creal(z3), cimag(z3));

    double complex z4 = 1 + 2 * I, z5 = 1 - 2 * I; // conjugates
    printf("(1+2i)*(1-2i) = %.1f%+.1fi\n", creal(z4 * z5), cimag(z4 * z5));
}
```

Output:

```text
I * I = -1.0+0.0i
pow(I, 2) = -1.0+0.0i
exp(I*PI) = -1.0+0.0i
(1+2i)*(1-2i) = 5.0+0.0i
```

### References

- C23 standard (ISO/IEC 9899:2024):

  - 6.10.8.3/1/2 `__STDC_NO_COMPLEX__` (p: TBD)

  - 6.10.8.3/1/2 `__STDC_IEC_559_COMPLEX__` (p: TBD)

  - 7.3 Complex arithmetic `<complex.h>` (p: TBD)

  - 7.25 Type-generic math `<tgmath.h>` (p: TBD)

  - 7.31.1 Complex arithmetic `<complex.h>` (p: TBD)

  - Annex G (normative) IEC 60559-compatible complex arithmetic (p: TBD)

- C17 standard (ISO/IEC 9899:2018):

  - 6.10.8.3/1/2 `__STDC_NO_COMPLEX__` (p: 128)

  - 6.10.8.3/1/2 `__STDC_IEC_559_COMPLEX__` (p: 128)

  - 7.3 Complex arithmetic `<complex.h>` (p: 136-144)

  - 7.25 Type-generic math `<tgmath.h>` (p: 272-273)

  - 7.31.1 Complex arithmetic `<complex.h>` (p: 391)

  - Annex G (normative) IEC 60559-compatible complex arithmetic (p: 469-479)

- C11 standard (ISO/IEC 9899:2011):

  - 6.10.8.3/1/2 `__STDC_NO_COMPLEX__` (p: 177)

  - 6.10.8.3/1/2 `__STDC_IEC_559_COMPLEX__` (p: 177)

  - 7.3 Complex arithmetic `<complex.h>` (p: 188-199)

  - 7.25 Type-generic math `<tgmath.h>` (p: 373-375)

  - 7.31.1 Complex arithmetic `<complex.h>` (p: 455)

  - Annex G (normative) IEC 60559-compatible complex arithmetic (p: 532-545)

- C99 standard (ISO/IEC 9899:1999):

  - 6.10.8/2 `__STDC_IEC_559_COMPLEX__` (p: 161)

  - 7.3 Complex arithmetic `<complex.h>` (p: 170-180)

  - 7.22 Type-generic math `<tgmath.h>` (p: 335-337)

  - 7.26.1 Complex arithmetic `<complex.h>` (p: 401)

  - Annex G (informative) IEC 60559-compatible complex arithmetic (p: 467-480)

### See also

- [C++ documentation for Complex number arithmetic](https://en.cppreference.com/w/cpp/numeric/complex)

---
Attribution: content from https://en.cppreference.com/w/c/numeric/complex (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.