<complex.h> header file in C with Examples

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.

Macro Name Expands To
complex _Complex
imaginary _Imaginary
_Complex_I (const float _Complex) i
_Imaginary_I (const float _Imaginary) i
I _Imaginary_I(_Complex_I if _Imaginary_I is absent)

Below program helps to create complex numbers.

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
}
chevron_right

Output:
z = 1.3+4.9i

Explanation:

Example 2: We can also create complex number objects using macro I.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
}
chevron_right

Output:
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.

Function Description
float cabsf(float complex arg)
double cabs(double complex arg)
long double cabsl(long double complex arg)
It returns the absolute value of complex argument
float complex cacosf(float complex arg)
double complex cacos(double complex arg)
long double complex cacosl(long double complex arg)
It returns the complex arc cosine values of complex argument
float complex cacoshf(float complex arg)
double complex cacosh(double complex arg)
long double complex cacoshl(long double complex arg)
It returns the complex arc hyperbolic cosine values of complex argument
float cargf(float complex arg)
double carg(double complex arg)
long double cargl(long double complex arg)
It returns the phase angle of complex argument (in radians).
float complex casinf(float complex arg)
double complex casin(double complex arg)
long double complex casinl(long double complex arg)
It returns the complex arc sine values of complex argument
float complex casinhf(float complex arg)
double complex casin(double complex arg)
long double complex casinl(long double complex arg)
It returns the complex arc hyperbolic sine values of complex argument
float complex catanf(float complex arg)
double complex catan(double complex arg)
long double complex catanl(long double complex arg)
It returns the complex arc tangent values of complex argument
float complex catanhf(float complex arg)
double complex catan(double complex arg)
long double complex catanl(long double complex arg)
It returns the complex arc hyperbolic tangent values of complex argument
float complex ccosf(float complex arg)
double complex ccos(double complex arg)
long double complex ccosl(long double complex arg)
It returns the complex cosine values of complex argument
float complex cexpf(float complex arg)
double complex cexp(double complex arg)
long double complex cexpl(long double complex arg)
It returns the complex value earg where e is the natural logarithm base
float crealf(float complex arg)
double creal(double complex arg)
long double creall(long double complex arg)
It returns the real part of the complex argument
float cimagf(float complex arg)
double cimag(double complex arg)
long double cimagl(long double complex arg)
It returns the imaginary part of the complex argument
float complex clogf(float complex arg)
double complex clog(double complex arg)
long double complex cimagl(long double complex arg)
It returns the imaginary part of the complex argument
float complex conjf(float complex arg)
double complex conj(double complex arg)
long double complex conjl(long double complex arg)
It returns the conjugate of complex argument
float complex cpowf(float complex a, long double complex b) It returns the complex value of ab
float complex csqrtf(float complex arg)
double complex csqrt(double complex arg)
long double complex csqrtl(long double complex arg)
It returns the complex square root of argument

Examples 3: Program to find Conjugate of a complex number.

filter_none

edit
close

play_arrow

link
brightness_4
code

#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));
}
chevron_right

Output:
z = 1.3-4.9i

Examples 4: Program to find the absolute value of a complex number.

filter_none

edit
close

play_arrow

link
brightness_4
code

#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));
}
chevron_right

Output:
Absolute value = 5.1

Examples 4: Program to find the phase angle of a complex number.

filter_none

edit
close

play_arrow

link
brightness_4
code

#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));
}
chevron_right

Output:
Phase Angle = 1.3 radians

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





**Passionate about things** **A Quick Learner** **Always strive for Perfection** **Campus Mantri and Technical Content Writer**

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
C
Practice Tags :