C sqrt() Function

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

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.

C
sqrt(4.0);
CallWhat it does
sqrt(4.0)Returns 2.

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

Library declarations
C
double sqrt(double x);
float sqrtf(float x);
long double sqrtl(long double x);

Parameters

ParameterDescription
xRequired. A double that is 0 or greater.

Return Value

Return ValueDescription
Square rootA non-negative double. sqrt(4) is 2.
NaNReturned when x is negative.

C Examples

Example 1 — Perfect and Irrational Roots

Print square roots for common values, including √2.

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

How It Works

  1. Include math.h. Declares sqrt; link with -lm on GCC.
  2. Zero and perfect squares. sqrt(0) is 0; sqrt(9) is 3.
  3. Irrational root. sqrt(2) is irrational ≈ 1.41421.
  4. 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.

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

How It Works

  1. Square the legs. 3² + 4² = 25.
  2. Take sqrt. sqrt(25) is 5.
  3. Same with hypot. hypot(3, 4) also returns 5.
  4. 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.

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

How It Works

  1. Validate first. x = -4 is outside the domain.
  2. Skip sqrt. Calling it would produce NaN.
  3. Report clearly. A message is easier to debug than a quiet NaN.
  4. 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.

Next: tan()

Compute the tangent of an angle in radians.

tan() 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