sqrt() — Calculate Square Root
Format
#include <math.h>
double sqrt(double x);
Language Level
ANSI
Threadsafe
Yes
Description
The sqrt()
function
calculates the nonnegative value of the square root of x.
Return Value
The sqrt()
function
returns the square root result. If x is
negative, the function sets errno to EDOM, and returns 0.
Example
This example computes the square
root of the quantity that is passed as the first argument to main.
It prints an error message if you pass a negative value.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char ** argv)
{
char * rest;
double value;
if ( argc != 2 )
printf( "Usage: %s value\n", argv[0] );
else
{
value = strtod( argv[1], &rest);
if ( value < 0.0 )
printf( "sqrt of a negative number\n" );
else
printf("sqrt( %lf ) = %lf\n", value, sqrt( value ));
}
}
/******************** If the input is 45, *****************************
**************** then the output should be similar to: **********
sqrt( 45.000000 ) = 6.708204
*/