C Math Functions
Math Functions
There is also a list of math functions available, that allows you to perform mathematical tasks on numbers.
To use them, you must include the math.h
header file in your program:
#include <math.h>
Square Root
To find the square root of a number, use the sqrt()
function:
Note: The math functions return a double value, so we use %f to print the result. That is why sqrt(16) prints 4.000000 and not 4.
Round a Number
The ceil() function rounds a number upwards to its nearest integer, and the floor() function rounds a number downwards to its nearest integer, and returns the result:
Example
printf("%f\n", ceil(1.4)); // 2.000000
printf("%f\n", floor(1.4)); // 1.000000
Try it Yourself »
Power
The pow() function returns the value of x to the power of
y (xy):
Complete Math Reference
For a complete reference of math functions, go to our C <math.h> Library Reference.