The conj()
function in C reverses the sign of the imaginary part of a complex number to compute its conjugate.
To use the conj()
function, include the complex.h
library as shown below:
#include <complex.h>
The conj()
function is declared as follows:
double complex conj(double complex z);
The conj()
function takes in the complex number z
as a parameter and returns the conjugate of z
.
Consider the code snippet below, which uses conj()
to compute the conjugate of a complex number:
#include <stdio.h>#include <complex.h>int main() {double complex complexNumber1 = 1 + I*2;double complex complexNumber2 = 1 - I*2;double complex conjugate1 = conj(complexNumber1);double complex conjugate2 = conj(complexNumber2);printf("conj ( %f + %fi ) = ( %f + %fi ) \n", creal(complexNumber1), cimag(complexNumber1) , creal(conjugate1), cimag(conjugate1));printf("conj ( %f + %fi ) = ( %f + %fi ) \n", creal(complexNumber2), cimag(complexNumber2) , creal(conjugate2), cimag(conjugate2));return 0;}
The conj()
function is used in lines 9 and 10 to compute the conjugate of the complex numbers complexNumber1
and complexNumber2
that are created in lines 6 and 7.
Free Resources