C hypot() Function

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

Definition

In C, hypot() returns the length of the hypotenuse for the sides x and y. That is √(x² + y²). hypot(3, 4) is 5.

You pass two double values, and both are required. The result is a non-negative double. Include <math.h>.

Syntax

Pass the two sides. Start with this call.

C
hypot(3.0, 4.0);
CallWhat it does
hypot(3.0, 4.0)Returns 5, the distance for sides 3 and 4.

Include <math.h>. On GCC, link with -lm.

Library declarations
C
double hypot(double x, double y);
float hypotf(float x, float y);
long double hypotl(long double x, long double y);

Parameters

ParameterDescription
xRequired. One side of the right triangle.
yRequired. The other side.

Return Value

Return ValueDescription
DistanceA non-negative double, √(x² + y²).

C Examples

Example 1 — Basic Hypotenuse

Compute the long side of a right triangle with legs 3 and 4.

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

int main(void) {
    printf("%.1f\n", hypot(3.0, 4.0));
    printf("%.1f\n", hypot(-3.0, 4.0));
    return 0;
}

How It Works

  1. Include math.h. That declares hypot.
  2. 3-4-5. √(9 + 16) is 5.
  3. Negative leg. Squaring removes the sign, so the length is still 5.
  4. Print as double. %.1f shows one decimal place.

Example 2 — Distance Between Two Points

Pass the differences in x and y to get Euclidean distance in the plane.

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

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

    printf("distance = %.1f\n", d);
    return 0;
}

How It Works

  1. Δx and Δy. 4-1 = 3 and 6-2 = 4.
  2. Call hypot. Same as hypot(3, 4) → 5.
  3. No absolute values needed. Signs of the deltas do not change the distance.
  4. Common pattern. Use this for collision checks, maps, and geometry.

Example 3 — Prefer hypot Over Naive sqrt

For huge legs, x*x can overflow even when the true hypotenuse still fits in a double.

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

int main(void) {
    double x = 1e200;
    double y = 1e200;

    printf("hypot = %g\n", hypot(x, y));
    /* sqrt(x*x + y*y) may become inf here */
    return 0;
}

How It Works

  1. Large legs. Each is 1e200.
  2. hypot scales first. It avoids computing (1e200)² directly as an overflowing square.
  3. Finite result. About √2 × 1e200.
  4. Takeaway. Use hypot whenever overflow of intermediate squares matters.

Frequently Asked Questions

hypot() returns the hypotenuse for sides x and y. hypot(3, 4) is 5.
Both are required. x and y are the two sides.
A non-negative double, the square root of x*x + y*y.
Include <math.h>. On GCC, link with -lm.
The squares can overflow for large x or y. hypot() avoids that intermediate overflow.

Next: ldexp()

Scale a mantissa by multiplying with 2 raised to an exponent.

ldexp() tutorial →

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