<complex.h> header file in C with Examples

07 Apr, 2020
Most of the C Programs deals with complex number operations and manipulations by using complex.h header file. This header file was added in C99 Standard.
C++ standard library has a header, which implements complex numbers as a template class, complex<T>, which is different from <complex.h> in C.
Macros associated with <complex.h>Some of the macros of <complex.h> are shown below. The values in the left side describe the Macros in complex.h and the right side describes the expansion of those macros with the keywords (_Imaginary, _Complex) added in C99 standard.
Below program helps to create complex numbers.
Example 1:
// C program to show the working// of complex.h library  #include <complex.h>#include <stdio.h>  int main(void){    double real = 1.3,           imag = 4.9;    double complex z        = CMPLX(real, imag);    printf(        "z = %.1f% + .1fi\n",        creal(z), cimag(z));}
z = 1.3+4.9i

Explanation:
cmplx() function creates complex number objects by taking real part and imaginary parts as parameters. This function returns the object of complex numbers.
creal() function returns the real part of a complex number
cimag() function returns the imaginary part of a complex number
If our real and imaginary parts are of type float we use cmplxf() function to generate complex numbers and to get real and imaginary parts we use crealf(), cimagf() functions.
If our real and imaginary parts are of type long double we use cmplxl() function to generate complex numbers and to get real and imaginary parts we use creall(), cimagl() functions.
Example 2: We can also create complex number objects using macro I.
// C program to create a complex// number using macro I  #include <complex.h>#include <stdio.h>  int main(void){    double complex        z        = 3.2 + 4.1 * I;      // Creates complex number    // with 3.2 and 4.1 as    // real and imaginary parts    printf(        "z = %.1f% + .1fi\n",        creal(z), cimag(z));}
z = 3.2+4.1i

Functions associated with <complex.h>The <complex.h> header file also provides some inbuilt functions to work with the complex number. Here the word “arg” stands for complex number object.
Examples 3: Program to find Conjugate of a complex number.
#include <complex.h>#include <stdio.h>  int main(void){    double real = 1.3,           imag = 4.9;    double complex z        = CMPLX(real, imag);    double complex        conj_f        = conjf(z);    printf("z = %.1f% + .1fi\n",           creal(conj_f),           cimag(conj_f));}
z = 1.3-4.9i

Examples 4: Program to find the absolute value of a complex number.
#include <complex.h>#include <stdio.h>  int main(void){    double real = 1.3,           imag = 4.9;    double complex z        = CMPLX(real, imag);    printf("Absolute value = %.1f",           cabsf(z));}
Absolute value = 5.1

Examples 4: Program to find the phase angle of a complex number.
#include <complex.h>#include <stdio.h>  int main(void){    double real = 1.3,           imag = 4.9;    double complex z        = CMPLX(real, imag);    printf(        "Phase Angle = %.1f radians\n",        cargf(z));}
Phase Angle = 1.3 radians

C-Library
C Language
C Programs
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Functions that cannot be overloaded in C++
Exception Handling in C++
Switch Statement in C/C++
Storage Classes in C
Operators in C / C++
Strings in C
size of char datatype and char array in C
UDP Server-Client implementation in C
Arrow operator -> in C/C++ with Examples
C Program to read contents of Whole File