TechBuzzPro

Square Root in C: Code & Examples

· 4 min read · Apps & Browsers

In this article
  1. Formula
  2. Worked Example
  3. How to Use sqrt() in Your Code
  4. Integer vs. Double Input
  5. Common Mistakes and Pitfalls
  6. Alternatives to sqrt()
  7. Performance and Accuracy Considerations
  8. Frequently Asked Questions

To compute a square root in C, use the sqrt() function from the math.h header: double result = sqrt(x);. You pass the number as a double, and it returns the square root as a double; for integer input, cast it to double first.

in x | Number | - | 25 | 0 | 1e15
out r | Square Root | - | sqrt(x) | 6

Formula

The mathematical square root of a number n is the value r such that r² = n. In C, the function sqrt(n) returns this value as a double. The formula is simply:

r = √n

where n is the input (must be ≥ 0 for real results), and r is the result. The function uses IEEE 754 floating-point arithmetic, so the result is accurate to about 15–16 decimal digits for typical double values.

Worked Example

Using the default input of 25:

  1. Include math.h and link with -lm (on Unix-like systems).
  2. Call sqrt(25.0).
  3. The function returns 5.0, because 5 × 5 = 25.

In code: double result = sqrt(25.0); → result = 5.0.

How to Use sqrt() in Your Code

To use sqrt(), you must include the math header and link the math library. Here’s a minimal complete program:

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

int main() {
    double x = 25.0;
    double r = sqrt(x);
    printf("Square root of %.2f is %.6f\n", x, r);
    return 0;
}

Compile with gcc program.c -lm to link the math library. Without -lm, you may get an “undefined reference to sqrt” error on Linux/macOS.

Integer vs. Double Input

If you have an integer, you can pass it directly because C will implicitly convert it to double. However, for very large integers (beyond 2^53), precision may be lost. For exact integer square roots (e.g., checking if a number is a perfect square), you can use sqrt() and then compare r*r to the original, but beware of floating-point rounding. A safer method for integers is to use an integer binary search or the sqrtl() function for long double.

Common Mistakes and Pitfalls

  • Forgetting to link -lm: On Unix-like systems, the math library is separate; without it, the linker fails.
  • Passing a negative number: sqrt(-1) returns NaN (Not a Number) and sets errno to EDOM. Always check input or use fabs() if appropriate.
  • Ignoring the return type: sqrt() returns double; if you assign to a float, you may lose precision.
  • Using ^ for exponentiation: In C, ^ is bitwise XOR, not power. Use pow(x, 0.5) or sqrt(x).

Alternatives to sqrt()

For specific needs, you might use:

  • sqrtf() for float (less precision, faster on some hardware).
  • sqrtl() for long double (more precision).
  • cbrt() for cube roots.
  • pow(x, 0.5) – but sqrt() is usually faster and more accurate.
  • For integer square roots, you can implement a binary search or use the isqrt from some libraries (not standard).

Performance and Accuracy Considerations

The sqrt() function is typically implemented in hardware on modern CPUs, so it is very fast. Accuracy is within 1 ULP (unit in the last place) for most implementations, meaning the result is correctly rounded. For financial or scientific applications where exact rounding matters, use sqrtl() or a decimal library. For embedded systems with no FPU, you might use a software approximation like Newton's method, but that is rarely needed.

Frequently Asked Questions

How do I get an integer square root in C?

Cast the result to int: int r = (int)sqrt(x);. But for large numbers, floating-point rounding may cause off-by-one errors. A safer integer method is to use a loop or binary search. For example, to check if 25 is a perfect square, compute int r = (int)sqrt(25), then check if r*r == 25.

Why does sqrt(-1) return a weird number?

sqrt(-1) returns NaN (Not a Number) because the square root of a negative number is not real. The function sets errno to EDOM (domain error). To handle this, check if the input is negative before calling, or use fabs() if you want the absolute value.

What is the difference between sqrt, sqrtf, and sqrtl?

They compute the square root for double, float, and long double respectively. sqrtf may be faster on some systems but less precise; sqrtl offers more precision but may be slower. Use the one matching your variable type to avoid implicit conversions.

Can I use sqrt without including math.h?

No, sqrt() is declared in math.h. Without the declaration, the compiler will assume it returns int, causing incorrect results. Always include #include <math.h>.

How do I compile a C program with sqrt?

Use gcc program.c -lm on Linux/macOS. On Windows with MinGW, the library is usually linked automatically, but adding -lm is harmless. If you use an IDE, ensure the math library is in the linker settings.