C Programming/C Reference/complex.h/carg

From Wikibooks, open books for an open world
Jump to: navigation, search

carg is a standard library function which calculates the argument (phase angle) of a complex number. It takes a single parameter and returns the phase angle of that value (converted if necessary to a complex number) within the range [π, −π].


This function takes a single parameter of type complex and returns a result of type double.

A complex number can be represented as:

  • Z = A + Bi (in rectangular co-ordinates)

where A = creal(Z) is the real part and B = cimag(Z) is the imaginary part.

  • z = re^{ia} (in polar co-ordinates)

where r = cabs(z) is the radius and a = carg(z) is the phase angle. Hence this function returns phase angle of complex number Z used in polar co-ordinates. The return value is in radians, in the range [π, -π]. When we use this function, we must include the <complex.h> header file.


#include <complex.h>
double carg(double complex z);
float cargf(float complex z);
long double cargl(long double complex z);[1]

Example 1[edit]

/* conversion of a real number from its Cartesian to polar form */

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

int main() {
double complex z = -4.4 + 3.3 * I;
double x = creal(z);
double y = cimag(z);

double radius = cabs(z);
double argument = carg(z);

printf("cartesian(x, y): (%4.1f, %4.1f)\n", x, y); printf("polar(r, theta): (%4.1f, %4.1f)\n", radius, argument);
return 0;


Cartesian(x, y): (-4.4, 3.3)
polar(r, theta): (5.5, 2.5)[2]