Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

sqrt — square root

The sqrt function from <math.h> computes the nonnegative square root. It accepts and returns double; an argument less than zero causes a domain error.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <math.h>
double sqrt(double x);

sqrt(x) computes the nonnegative square root of x. Include <math.h> before calling it. The argument and result have type double; this is not an integer square-root operation.

An argument less than zero causes a domain error. Portable code must not assume a particular result value for that call. The related functions sqrtf and sqrtl accept and return float and long double, respectively.

The example computes sqrt(25.0) and prints the result with one digit after the decimal point. The expected output is 5.0 followed by a newline.

Examples

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

int main(void)
{
    double x = 25.0;
    double result = sqrt(x);
    printf("%.1f\n", result);
    return 0;
}

Result

5.0

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources