C atan2() Function

Definition
In C, atan2() returns the angle from the positive x-axis to the point (x, y). The angle is in radians, from -π to π. atan2(1, 1) is π/4.
You pass two double values, and both are required. The first is y and the second is x. Include <math.h>.
Syntax
Pass y first, then x. Start with this call.
| Call | What it does |
|---|
atan2(1.0, 1.0) | Returns π/4 for the point (1, 1). |
Include <math.h>. On GCC, link with -lm. The result is radians.
Library declarations
double atan2(double y, double x);
float atan2f(float y, float x);
long double atan2l(long double y, long double x);
Parameters
| Parameter | Description |
|---|
| y | Required. The vertical coordinate. This argument comes first. |
| x | Required. The horizontal coordinate. This argument comes second. |
Return Value
| Return Value | Description |
|---|
| Radians | The angle from -π to π for the point (x, y). |
C Examples
Example 1 — Angles for Common Points
Print radians for points on the axes and on the diagonal.
#include <math.h>
#include <stdio.h>
int main(void) {
printf("atan2(0, 1) = %.4f\n", atan2(0.0, 1.0));
printf("atan2(1, 1) = %.4f\n", atan2(1.0, 1.0));
printf("atan2(1, 0) = %.4f\n", atan2(1.0, 0.0));
printf("atan2(0, -1) = %.4f\n", atan2(0.0, -1.0));
return 0;
}
atan2(0, 1) = 0.0000
atan2(1, 1) = 0.7854
atan2(1, 0) = 1.5708
atan2(0, -1) = 3.1416
How It Works
- Include math.h. Declares
atan2; link with -lm on GCC. - Positive x-axis.
atan2(0, 1) is 0. - Diagonal and north.
atan2(1, 1) → π/4; atan2(1, 0) → π/2. - Negative x-axis.
atan2(0, -1) is π (180°), which a bare atan(y/x) cannot report correctly.
Example 2 — Convert Radians to Degrees
Multiply by 180 / π when you want a compass-style degree reading.
#include <math.h>
#include <stdio.h>
int main(void) {
double x = -1.0;
double y = 1.0;
double rad = atan2(y, x);
double deg = rad * 180.0 / acos(-1.0);
printf("point (%.0f, %.0f)\n", x, y);
printf("atan2 = %.4f rad\n", rad);
printf(" = %.2f deg\n", deg);
return 0;
}
point (-1, 1)
atan2 = 2.3562 rad
= 135.00 degHow It Works
- Second quadrant.
x < 0 and y > 0. - Call with y, x.
atan2(1, -1) returns about 2.356 (3π/4). - Convert for display.
* 180 / π → 135°. - π from acos.
acos(-1.0) is a portable π without M_PI.
Example 3 — Why Not atan(y/x)?
When x is negative, dividing first loses the quadrant. atan2 keeps it.
#include <math.h>
#include <stdio.h>
int main(void) {
double x = -1.0;
double y = 1.0;
printf("atan(y/x) = %.4f\n", atan(y / x));
printf("atan2(y,x) = %.4f\n", atan2(y, x));
return 0;
}
atan(y/x) = -0.7854
atan2(y,x) = 2.3562
How It Works
- Division collapses signs.
y/x = 1/-1 = -1. - atan sees only -1. It returns
-π/4 (wrong quadrant for this point). - atan2 keeps both. It returns
3π/4 for (-1, 1). - Also safer at x = 0.
y/x would divide by zero; atan2(y, 0) is fine.
Frequently Asked Questions
atan2() returns the angle, in radians, from the positive x-axis to the point (x, y).
Both are required. y comes first and x comes second.
A double in radians, from -π to π.
Include <math.h>. On GCC, link with -lm.
The call is atan2(y, x). Swapping the arguments changes the angle.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful