C hypot() Function

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.
| Call | What 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
double hypot(double x, double y);
float hypotf(float x, float y);
long double hypotl(long double x, long double y);
Parameters
| Parameter | Description |
|---|
| x | Required. One side of the right triangle. |
| y | Required. The other side. |
Return Value
| Return Value | Description |
|---|
| Distance | A non-negative double, √(x² + y²). |
C Examples
Example 1 — Basic Hypotenuse
Compute the long side of a right triangle with legs 3 and 4.
#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
- Include math.h. That declares
hypot. - 3-4-5. √(9 + 16) is 5.
- Negative leg. Squaring removes the sign, so the length is still 5.
- 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.
#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
- Δx and Δy.
4-1 = 3 and 6-2 = 4. - Call hypot. Same as
hypot(3, 4) → 5. - No absolute values needed. Signs of the deltas do not change the distance.
- 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.
#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
- Large legs. Each is
1e200. - hypot scales first. It avoids computing
(1e200)² directly as an overflowing square. - Finite result. About
√2 × 1e200. - 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
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