C Math hypot() Function

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
<math.h>

What You’ll Learn

The hypot() function computes √(x² + y²)—the hypotenuse of a right triangle with legs x and y. It is the standard way to measure 2D distance in graphics, games, and geometry, with built-in protection against overflow.

01

√(x²+y²)

Pythagoras.

02

math.h

Link -lm.

03

3-4-5

Classic.

04

Distance

2D points.

05

Safe

No overflow.

06

vs sqrt

Prefer hypot.

Definition and Usage

The Pythagorean theorem says that for a right triangle with legs x and y, the hypotenuse is √(x² + y²). In C, hypot(x, y) computes this directly.

Geometrically, hypot(x, y) is the straight-line distance from the origin (0, 0) to the point (x, y). For two points (x1, y1) and (x2, y2), use hypot(x2 - x1, y2 - y1).

💡
Beginner Tip

Writing sqrt(x*x + y*y) looks fine for small numbers like 3 and 4, but squaring huge coordinates can overflow. hypot scales the calculation internally—use it whenever distance matters in real code.

📝 Syntax

Standard C declaration:

C
double hypot(double x, double y);

Related variants

C
float hypotf(float x, float y);           /* <math.h> */
long double hypotl(long double x, long double y); /* <math.h> */

Parameters

  • x — first leg (horizontal offset or x-coordinate).
  • y — second leg (vertical offset or y-coordinate).

Return Value

  • √(x² + y²) as a non-negative double.
  • hypot(3, 4) returns 5.0.
  • hypot(0, y) returns |y|; hypot(x, 0) returns |x|.

Headers and linking

  • #include <math.h>
  • Compile: gcc hypot.c -std=c11 -o hypot -lm

⚡ Quick Reference

CallMeaningResult
hypot(3, 4)3-4-5 triangle5.0
hypot(5, 12)5-12-13 triangle13.0
hypot(-3, 4)Sign ignored5.0
hypot(dx, dy)Point distanceEuclidean
sqrt(x*x+y*y)Manual (risky)Same if safe
Hypotenuse
hypot(x, y)

2D length

Two points
hypot(x2-x1, y2-y1)

Distance

1D only
fabs(a - b)

Line dist

Manual
sqrt(x*x + y*y)

Avoid large x,y

Examples Gallery

Compile every example with gcc file.c -std=c11 -o out -lm. Distance calculations appear constantly in graphics and collision detection.

📚 Getting Started

Calculate the hypotenuse of a classic right triangle.

Example 1 — Classic 3-4-5 Triangle

Find the hypotenuse when legs are 3 and 4.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double side1 = 3.0;
    double side2 = 4.0;
    double h = hypot(side1, side2);

    printf("Legs: %.1f and %.1f\n", side1, side2);
    printf("Hypotenuse: %.1f\n", h);

    return 0;
}

How It Works

3² + 4² = 9 + 16 = 25, and √25 = 5. hypot(3, 4) does this in one call.

Example 2 — Distance Between Two Points

How far is (1, 2) from (4, 6)?

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double x1 = 1.0, y1 = 2.0;
    double x2 = 4.0, y2 = 6.0;
    double dx = x2 - x1;
    double dy = y2 - y1;
    double dist = hypot(dx, dy);

    printf("Point A: (%.0f, %.0f)\n", x1, y1);
    printf("Point B: (%.0f, %.0f)\n", x2, y2);
    printf("Distance: %.1f\n", dist);

    return 0;
}

How It Works

dx = 3, dy = 4—another 3-4-5 triangle hidden inside the coordinate difference.

📈 Practical Patterns

Sign rules, edge cases, and comparison with manual sqrt.

Example 3 — Negative Legs Give the Same Result

Squaring removes the sign.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    printf("hypot( 3,  4) = %.1f\n", hypot( 3.0,  4.0));
    printf("hypot(-3,  4) = %.1f\n", hypot(-3.0,  4.0));
    printf("hypot( 3, -4) = %.1f\n", hypot( 3.0, -4.0));
    printf("hypot(-3, -4) = %.1f\n", hypot(-3.0, -4.0));

    return 0;
}

How It Works

Pass coordinate differences directly—no need to call fabs on each leg first.

Example 4 — One Leg Is Zero

When one side is 0, hypot returns the other side’s magnitude.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    printf("hypot(0,  7) = %.1f\n", hypot(0.0,  7.0));
    printf("hypot(0, -7) = %.1f\n", hypot(0.0, -7.0));
    printf("hypot(5,  0) = %.1f\n", hypot(5.0,  0.0));

    return 0;
}

How It Works

With a zero leg, the triangle collapses to a line—distance equals the absolute value of the other leg.

Example 5 — hypot() vs sqrt(x² + y²)

Both work for small values; hypot is safer at scale.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double x = 3.0;
    double y = 4.0;
    double manual = sqrt(x * x + y * y);
    double library = hypot(x, y);

    printf("sqrt(x*x + y*y) = %.6f\n", manual);
    printf("hypot(x, y)     = %.6f\n", library);

    return 0;
}

How It Works

Results match for 3 and 4. For very large coordinates (e.g. map tiles in millions), x*x can overflow to infinity while hypot still returns a finite distance.

🚀 Common Use Cases

  • Graphics and games — distance between sprites, click points, or enemies.
  • Collision detection — circular hit radius checks.
  • GPS / mapping — approximate planar distance from coordinate deltas.
  • Physics — vector magnitude in 2D simulations.
  • Geometry homework — right-triangle side calculations.

🧠 How hypot() Works

1

Receive x and y

Leg lengths or coordinate differences (any sign).

Input
2

Scale safely

Avoid overflow when squaring large values.

Compute
3

Return √(x²+y²)

Non-negative distance as double.

Output
=

Hypotenuse / distance

Compare to a radius: hypot(dx,dy) < r.

📝 Notes

  • Computes √(x² + y²), always non-negative.
  • Sign of x and y does not affect the result.
  • Designed to avoid overflow/underflow vs naive squaring.
  • For 1D distance on a line, fabs(a - b) is enough.
  • 3D distance: some platforms offer non-standard helpers; otherwise combine two hypot calls carefully.
  • Link with -lm on GCC/Clang Unix-like builds.

⚡ Optimization

hypot() costs more than a few multiplications but buys numerical stability. In inner loops comparing squared distances, some games compare dx*dx + dy*dy < r*r to skip sqrt—but when you need the actual length, use hypot or sqrt of the summed squares only if values are known small.

Conclusion

hypot() computes the Pythagorean length √(x² + y²) safely—ideal for triangle hypotenuses and 2D point distance. Prefer it over manual sqrt(x*x + y*y) when coordinates can be large.

Continue with ldexp() for scaling by powers of two, or review fabs() for 1D distance.

💡 Best Practices

✅ Do

  • Use hypot(dx, dy) for 2D distance
  • Prefer hypot over sqrt(x*x+y*y) for large values
  • Use hypotf when float precision is enough
  • Compare squared distance in hot loops when only ordering matters
  • Include <math.h> and link -lm

❌ Don’t

  • Square huge coordinates without considering overflow
  • Manually abs() each leg before hypot (unnecessary)
  • Use hypot for 1D when fabs suffices
  • Forget hypot returns length, not squared length
  • Assume 3D hypot exists in standard C (it does not)

Key Takeaways

Knowledge Unlocked

Five things to remember about hypot()

Hypotenuse and distance in C, explained simply.

5
Core concepts
📚 02

3-4-5

hypot(3,4)=5.

Classic
📈 03

Distance

2D points.

Pattern
📄 04

Overflow safe

vs sqrt.

Why
🔄 05

Sign free

Squares.

Rule

❓ Frequently Asked Questions

hypot(x, y) returns the square root of x*x + y*y — the length of the hypotenuse of a right triangle with legs x and y. For example, hypot(3, 4) is 5.0. It also gives the distance from (0, 0) to (x, y).
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double hypot(double x, double y).
Squaring very large or very small values can overflow or underflow before sqrt runs. hypot() scales internally to compute sqrt(x^2 + y^2) more safely. Prefer hypot for robust distance math.
No. hypot uses squares, so hypot(-3, 4) equals hypot(3, 4), both 5.0. Pass raw coordinate differences without worrying about sign.
hypotf(x, y) takes float arguments and returns float. hypotl(x, y) uses long double. Match the variant to your variable types.
Use hypot(x2 - x1, y2 - y1). For example, distance from (1, 2) to (4, 6) is hypot(3, 4) = 5.
Did you know?

The 3-4-5 triangle is the smallest integer-sided right triangle. Ancient builders used knotted ropes with 12 evenly spaced knots to lay out perfect right angles—the same math hypot(3, 4) encodes in one line of C.

Explore C Math Functions

Continue with ldexp() or browse the full math function reference.

Math Functions Index →

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.

5 people found this page helpful