C sqrt() Function

Definition
In C, sqrt() returns the square root of x. sqrt(4) is 2, and sqrt(0) is 0.
You pass one double, and it must be 0 or greater. The result is a non-negative double. Include <math.h>.
Syntax
Pass a number that is 0 or greater. Start with this call.
| Call | What it does |
|---|
sqrt(4.0) | Returns 2. |
Include <math.h>. On GCC, link with -lm.
Library declarations
double sqrt(double x);
float sqrtf(float x);
long double sqrtl(long double x);
Parameters
| Parameter | Description |
|---|
| x | Required. A double that is 0 or greater. |
Return Value
| Return Value | Description |
|---|
| Square root | A non-negative double. sqrt(4) is 2. |
| NaN | Returned when x is negative. |
C Examples
Example 1 — Perfect and Irrational Roots
Print square roots for common values, including √2.
#include <math.h>
#include <stdio.h>
int main(void) {
printf("sqrt(0) = %.4f\n", sqrt(0.0));
printf("sqrt(9) = %.4f\n", sqrt(9.0));
printf("sqrt(2) = %.5f\n", sqrt(2.0));
printf("sqrt(25) = %.4f\n", sqrt(25.0));
return 0;
}
sqrt(0) = 0.0000
sqrt(9) = 3.0000
sqrt(2) = 1.41421
sqrt(25) = 5.0000
How It Works
- Include math.h. Declares
sqrt; link with -lm on GCC. - Zero and perfect squares.
sqrt(0) is 0; sqrt(9) is 3. - Irrational root.
sqrt(2) is irrational ≈ 1.41421. - Non-negative return. You always get
y >= 0.
Example 2 — Distance with sqrt
Classic 3-4-5 triangle. For production code, prefer hypot when values can be large.
#include <math.h>
#include <stdio.h>
int main(void) {
double a = 3.0;
double b = 4.0;
double d = sqrt(a * a + b * b);
printf("distance = %.1f\n", d);
printf("hypot = %.1f\n", hypot(a, b));
return 0;
}
distance = 5.0
hypot = 5.0
How It Works
- Square the legs.
3² + 4² = 25. - Take sqrt.
sqrt(25) is 5. - Same with hypot.
hypot(3, 4) also returns 5. - Why hypot? It avoids overflow when
a*a + b*b is huge.
Example 3 — Guard Against Negatives
Check the domain before calling sqrt so you never print NaN by accident.
#include <math.h>
#include <stdio.h>
int main(void) {
double x = -4.0;
if (x < 0.0) {
printf("domain error: x must be >= 0\n");
} else {
printf("sqrt(%.1f) = %.4f\n", x, sqrt(x));
}
return 0;
}
domain error: x must be >= 0
How It Works
- Validate first.
x = -4 is outside the domain. - Skip sqrt. Calling it would produce
NaN. - Report clearly. A message is easier to debug than a quiet NaN.
- Optional errno. See
errno.h for EDOM handling.
Frequently Asked Questions
sqrt() returns the square root of x. sqrt(4) is 2, and sqrt(0) is 0.
x is required. It must be a double that is 0 or greater.
A non-negative double. A negative x returns NaN.
Include <math.h>. On GCC, link with -lm.
It returns NaN. The square root of a negative number is not a real result.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
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