<math.h> LibraryIn C programming, performing complex mathematical operations from scratch can be tedious and prone to errors. Fortunately, C provides a robust, heavily optimized standard library header called <math.h> that includes a wide array of built-in mathematical functions.
Whether you need to calculate square roots, powers, trigonometric values, or perform precision rounding, the <math.h> library has you covered. In this guide, we will explore the most commonly used math functions in C with clear, actionable examples.
To use any of the math functions, you must first include the <math.h> header file at the top of your C program.
Note for Linux users: When compiling a C program that heavily relies on <math.h> using GCC, you often need to manually link the math library by adding the -lm flag at the end of your compilation command (e.g., gcc program.c -o program -lm).
#include <stdio.h> #include <math.h> // Required for advanced math functions
sqrt()The sqrt() function takes a double as an argument and calculates its square root. Passing a negative number to this function will result in a domain error.
#include <stdio.h> #include <math.h>int main() { double num = 25.0; double result = sqrt(num); printf("The square root of %.2f is %.2f\n", num, result); // Output: The square root of 25.00 is 5.00 return 0; }
pow()The pow(base, exponent) function calculates the value of the base raised to the power of the exponent. It handles both positive and negative exponents beautifully.
#include <stdio.h> #include <math.h>int main() { double base = 2.0; double exponent = 3.0; double result = pow(base, exponent); printf("%.2f raised to the power of %.2f is %.2f\n", base, exponent, result); // Output: 2.00 raised to the power of 3.00 is 8.00 return 0; }
When working with floating-point numbers (float or double), you frequently need to round them to the nearest integer. <math.h> offers three very distinct tools for this:
ceil()The ceil() function rounds a number up to the nearest integer, regardless of the decimal value.
floor()The floor() function rounds a number down to the nearest integer.
round()The round() function rounds a number to the nearest integer following standard mathematical rounding rules (e.g., .5 and above rounds up, below .5 rounds down).
#include <stdio.h> #include <math.h>int main() { double num1 = 4.3; double num2 = 4.8; printf("ceil(4.3) = %.1f\n", ceil(num1)); // Output: 5.0 printf("floor(4.8) = %.1f\n", floor(num2)); // Output: 4.0 printf("round(4.3) = %.1f\n", round(num1)); // Output: 4.0 printf("round(4.8) = %.1f\n", round(num2)); // Output: 5.0 return 0; }
abs()Used to find the absolute (positive) value of an integer.
Note: Unlike the rest of the functions here, abs() is actually a part of the <stdlib.h> library, not <math.h>!
fabs()Used to find the absolute value of a floating-point number (float or double). This is included directly in <math.h>.
#include <stdio.h> #include <math.h> #include <stdlib.h> // Required for integer abs()int main() { int intNum = -15; double doubleNum = -15.75; printf("Absolute of -15 is %d\n", abs(intNum)); printf("Absolute of -15.75 is %.2f\n", fabs(doubleNum)); return 0; }
C provides standard trigonometric functions like sin(), cos(), and tan().
Important Rule: These trigonometric functions expect the angle to be passed in radians, not degrees. To reliably convert degrees to radians, multiply the degrees by (PI / 180).
#include <stdio.h> #include <math.h>#define PI 3.14159265
int main() { double degrees = 90.0; // Convert degrees to radians mathematically double radians = degrees * (PI / 180.0); printf("The sine of %.1f degrees is %.4f\n", degrees, sin(radians)); // Output: 1.0000 return 0; }
<math.h> take double as arguments and return double. Be careful with unintended type conversion if you are working primarily with standard int or single-precision float.sqrt()) will result in a mathematical Domain Error. In such cases, the function will safely return a special system value called NaN (Not a Number).-lm on Linux/Unix systems if you face "undefined reference" errors to math functions in your terminal.Which function should you use to round the decimal number 7.1 strictly up to 8.0?