C atan2() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

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.

C
atan2(1.0, 1.0);
CallWhat 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
C
double atan2(double y, double x);
float atan2f(float y, float x);
long double atan2l(long double y, long double x);

Parameters

ParameterDescription
yRequired. The vertical coordinate. This argument comes first.
xRequired. The horizontal coordinate. This argument comes second.

Return Value

Return ValueDescription
RadiansThe 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.

C
#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;
}

How It Works

  1. Include math.h. Declares atan2; link with -lm on GCC.
  2. Positive x-axis. atan2(0, 1) is 0.
  3. Diagonal and north. atan2(1, 1) → π/4; atan2(1, 0) → π/2.
  4. 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.

C
#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;
}

How It Works

  1. Second quadrant. x < 0 and y > 0.
  2. Call with y, x. atan2(1, -1) returns about 2.356 (3π/4).
  3. Convert for display. * 180 / π → 135°.
  4. π 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.

C
#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;
}

How It Works

  1. Division collapses signs. y/x = 1/-1 = -1.
  2. atan sees only -1. It returns -π/4 (wrong quadrant for this point).
  3. atan2 keeps both. It returns 3π/4 for (-1, 1).
  4. 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.

Next: atanh()

Learn the inverse hyperbolic tangent from math.h.

atanh() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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