Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

How to use conjf() in C

Ayesha Naeem

The conjf() function in C calculates the conjugate of a complex number. For example: for the complex number 3+4i3 + 4i, the conjf() function will return 34i3 - 4i.

To use the conjf() function, the program needs to include the complex.h header file as shown below:

#include <complex.h>

Parameters and return value

The conjf() function accepts a single parameter of type float complex.

conjf() returns a single value of type float complex.

Examples

The following code demonstrates the use of conjf() to calculate the conjugate of a complex number:

#include <stdio.h>
#include <complex.h>

int main()
{
  // define complex  number
  float complex num = 4.3 + 9.1*I;
  //get real part
  float real = crealf(num);
  //get imaginary part
  float imag = cimagf(num);
  printf("num is %.1f% + .1fi\n",real, imag );
  //calculate conjugate.
  float complex conj_num= conjf(num);
  float conj_real = crealf(conj_num);
  float conj_imag = cimagf(conj_num);
  printf("Applying conjugation\n");
  printf("num is %.1f% + .1fi\n",conj_real, conj_imag );
}

Explanation

  • The program defines a complex number of type float, then obtains the real part through crealf() and the imaginary part through cimagf().
  • The conjf() function calculates the conjugate of the complex number.
  • An output statement in line 12 shows the original number, and an output statement in line 17 shows the conjugate of the complex number.

RELATED TAGS

c

CONTRIBUTOR

Ayesha Naeem
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring