atan2() Function Example in C Programming

In this source code example, we will see how to use the atan2() function in C programming with an example.

atan2() Function Overview

The atan2() function in C calculates the arctangent of the quotient of its arguments. Unlike atan(), which takes a single argument, atan2() requires two arguments: y and x, representing the coordinates in a two-dimensional plane. 

The result, in radians, represents the angle between the x-axis and a point (x, y). The range of values returned by atan2() spans from \([-π, π]\). It's important in applications such as determining the angle of rotation or converting Cartesian coordinates to polar coordinates. This function can be found in the math.h library. 

Key Points: 

- You should include the math.h header to use the atan2() function. 

- The atan2() function provides an angle based on the coordinates' position in all four quadrants, unlike the atan() function which is limited to two quadrants. 

- The function's result is in radians. To get the value in degrees, use the formula: degree = radian × 180 / π

- Remember to link the math library with the -lm flag when compiling.

Source Code Example

#include <stdio.h>
#include <math.h>  // Required for atan2()

int main() {
    double x, y, result_in_radians, result_in_degrees;

    // Get the coordinates from the user
    printf("Enter the x coordinate: ");
    scanf("%lf", &x);
    printf("Enter the y coordinate: ");
    scanf("%lf", &y);

    // Calculate the arctangent based on the coordinates
    result_in_radians = atan2(y, x);

    // Convert the result to degrees
    result_in_degrees = result_in_radians * 180.0 / M_PI;

    // Display the results
    printf("Arctangent of (y/x) for coordinates (%.2lf, %.2lf) in radians is: %.2lf\n", x, y, result_in_radians);
    printf("In degrees: %.2lf\n", result_in_degrees);

    return 0;


Enter the x coordinate: 1
Enter the y coordinate: 1
Arctangent of (y/x) for coordinates (1.00, 1.00) in radians is: 0.79
In degrees: 45.00


1. We begin by integrating the essential header files: stdio.h for I/O operations and math.h to access the atan2() function.

2. Within the main() function, we collect both x and y coordinates from the user.

3. The atan2() function is then used to determine the arctangent of the quotient of these coordinates.

4. The resulting radian value is converted to degrees for ease of understanding.

5. Both the radian and degree values are subsequently printed on the console.