![The C Standard Library Reference](/c_standard_library/images/c-mini-logo.jpg)
- The C Standard Library
- C Library - Home
- C Library - <assert.h>
- C Library - <complex.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <fenv.h>
- C Library - <float.h>
- C Library - <inttypes.h>
- C Library - <iso646.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdalign.h>
- C Library - <stdarg.h>
- C Library - <stdbool.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <tgmath.h>
- C Library - <time.h>
- C Library - <wctype.h>
- C Standard Library Resources
- C Library - Quick Guide
- C Library - Useful Resources
- C Library - Discussion
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
C library - carg() function
The C complex library carg() function is used to calculate the argument (phase angle) of a complex number. The phase angle of a complex number (z = x + jy) is the angle between the positive real axis and the line representing the complex number in the complex plane. It is denoted as π or θ
This function is depends on the type of z(complex number). If the z is of the "float" type or float imaginary, we can use cargf() to compute the phase angle, For type long double, use cargl(), and for type double, use carg().
Syntax
Following is the C library syntax of carg() function −
double carg( double complex z );
Parameters
This function accepts a single parameter −
-
Z − It represent a complex number for which we want to compute the phase angle.
Return Value
This function returns the phase angle of z in the interval [-π, π] if no errors occur.
Example 1
Following is the basic c program to demonstrate the use of carg() to calculate the phase angle of z.
#include <stdio.h> #include <complex.h> int main(void) { double complex z = 1.0 + 1.0 * I; printf("Phase angle of %.1f + %.1fi is %.6f\n", creal(z), cimag(z), carg(z)); return 0; }
Output
Following is the output −
Phase angle of 1.0 + 1.0i is 0.785398
Example 2
Let's see another example, we uses the carg() function to calculate the phase angle (argument) of the negative complex number.
#include <stdio.h> #include <complex.h> int main(void) { double complex z = -3.0 + 4.0*I; // Calculate the phase angle using carg() double phase_angle = carg(z); printf("Complex number: %.1f + %.1fi\n", creal(z), cimag(z)); printf("Phase angle: %.6f radians\n", phase_angle); return 0; }
Output
Following is the output −
Complex number: -3.0 + 4.0i Phase angle: 2.214297 radians
Example 3
The below example calculates the phase angle of the complex number that lies in the 3rd quadrant.
#include <stdio.h> #include <complex.h> int main(void) { double complex z = -3.0 + -4.0*I; // Calculate the phase angle using carg() double phase_angle = carg(z); printf("Complex number: %.1f + %.1fi\n", creal(z), cimag(z)); printf("Phase angle: %.6f radians\n", phase_angle); return 0; }
Output
Following is the output, which indicating that the phase angle lies in the third quadrant according to the range [-π, π].
Complex number: -3.0 + -4.0i Phase angle: -2.214297 radians