<complex.h> header file in C

The complex.h header file is used by most C programs to perform complex number operations and conversions. This header file is included in the C99 Standard.

The C++ standard library includes a header, complex<T>, that implements complex values as a template class, as compared to complex.<h> in C.

Macros related to <complex.h>

Some macros from <complex.h> are given below. The values on the left side explain the Macros in complex.h, and the values on the right side indicate the extension of those macros with the C99 standardized keywords (_Imaginary, _Complexvalue).

Macro NameExpands To
Complex_complex
Imaginary_Imaginary
_complex_I(const float _Complexvalue) i
_imaginay_I(const float _Imaginaryvalue) i

Example:

Let us take an example to illustrate the use of complex.h header file in C.

Output:

z = 1.8+7.9i

Explanation:

  • The cmplx() method generates complex number objects by passing arguments for real and imaginary portions. This method returns the complex number object.
  • The creal() method returns the complex number's real portion.
  • The method cimag() returns the imaginary component of a complex number.
  • If real and imaginary parts belong to the type float, we can use the cmplxf() function to construct complex numbers and the crealf() and cimagf() procedures to obtain real and imaginary parts.
  • If both real and imaginary parts belong to the type long double, we use the cmplxl() function to construct complex numbers and the creall() and cimagl() procedures to extract real and imaginary parts.

Example:

Let us take an example to illustrate the use of complex.h header file in C.

Output:

z1 = 3.9+4.8i

Explanation:

In this example, the printf function is used to show the complex number z1. Here's an explanation of the format specifier:

  • "z1 = ": This string will be shown exactly as it is.
  • %.1f: Use this format specifier to print the complex number's real portion (creal(z1)) with a single number after the end of the decimal point.
  • %+.1fi: The above format specifier is used to display the complex number's imaginary portion (cimag(z1). %+ is used to show the imaginary part's sign, and .1fi represents the imaginary component as one digit after the decimal point.

The <complex.h> header file also has several built-in functions for working with complex numbers. The term "argument" stands for complex number object in this example.

FunctionDescription
loat cabsf(float complex argument)When given a single-precision floating-point number (float type), cabsf() computes the magnitude (absolute value) of a complex number.
It accepts a single float complex parameter and returns the magnitude of that parameter as a float.
double cabs(double complex argument)Whenever the input is a double-precision floating-point value (double type), cabs() calculates the magnitude of a complex number.
It accepts a double complex parameter and returns its magnitude as a double.
long double cabsl(long double complex argument)When given a long double-precision floating-point value (long double type), cabsl() computes the magnitude of a complex number.
It takes a long double complex input and returns its magnitude as a long double.
float complex cacosf(float complex arg)When given a single-precision floating-point value (float type), the cacosf() function in C computes the complex arc cosine of a complex variable. The result is returned as a complex number.
double complex cacos(double complex arg)When the input is a double-precision floating-point numbers number (double type), the cacos() function within C computes the complex number arc cosine of the complex number. The result is returned as a complex number.
float complex cacoshf(float complex arg)cacoshf() accepts a single float complex parameter. It calculates the complex arc hyperbolic cosine of the provided complex number. Both the input as well as output are in single-precision floating-point numbers format (float).
float cargf(float complex arg)When the input parameter is a single-precision floating-point integer (float type), the cargf() function in C computes the primary value of the argument (which is also referred to as the phase or angle) of a complex number. It returns the result as a single-precision floating-point numbers value (float).