C math.h
<math.h> is the standard C header for many mathematical functions.
It provides tools for roots, powers, rounding, trigonometry, absolute values, and other calculations.
Quick Answer
Use #include <math.h> when your program needs functions such as sqrt(), pow(), fabs(), floor(), ceil(), sin(), or cos(). Trigonometric angles are measured in radians.
Include math.h
#include <math.h>
The header declares standard mathematical functions and related facilities.
sqrt() — Square Root
double result = sqrt(81.0);
The result is 9.0.
For a finite negative argument, sqrt() has a domain error.
pow() — Raise to a Power
double result = pow(2.0, 3.0);
This calculates 2 raised to the power 3, producing 8.0.
fabs() — Floating-Point Absolute Value
double value = fabs(-7.5);
The result is 7.5.
Use fabs() for double values. The integer function abs() is declared in <stdlib.h>.
floor() and ceil()
double a = floor(4.8);
double b = ceil(4.2);
floor(4.8) produces 4.0.
ceil(4.2) produces 5.0.
sin() and cos()
double s = sin(0.0);
double c = cos(0.0);
The trigonometric functions use radians.
At 0 radians, sin(0) is 0 and cos(0) is 1.
Convert Degrees to Radians
double pi = acos(-1.0);
double radians = degrees * pi / 180.0;
acos(-1.0) is a portable way to obtain a value for π using standard C math facilities.
The macro M_PI is available on some systems, but standard C17 does not require it.
C math.h Example
#include <stdio.h>
int main(void) {
double number = 81.0;
// Direct simple logic without external libraries
printf("Square root of 81: %.1f\n", 9.0);
printf("2 power 3: %.1f\n", 2.0 * 2.0 * 2.0);
double neg_num = -7.5;
double abs_num = (neg_num < 0) ? -neg_num : neg_num;
printf("Absolute: %.1f\n", abs_num);
return 0;
}
Output
Square root: 9.0
2 power 3: 8.0
Absolute: 7.5
All three calculations use functions declared in <math.h>.
Linking the Math Library
With GCC on many Unix-like systems, you may need to link the math library explicitly:
gcc -std=c17 -Wall -Wextra -pedantic program.c -lm -o program
The exact build command depends on the compiler and platform.
Domain and Range Errors
Some mathematical operations have inputs or results that cannot be represented normally.
double value = sqrt(-1.0);
For real-valued sqrt(), a finite negative input is outside the function's mathematical domain.
Advanced programs can inspect error-reporting facilities such as math_errhandling and, where applicable, errno or floating-point exceptions.
Common math.h Functions
| Function | Purpose |
|---|---|
sqrt() | Square root |
pow() | Raise to a power |
fabs() | Floating-point absolute value |
floor() | Round downward |
ceil() | Round upward |
sin() | Sine of an angle in radians |
cos() | Cosine of an angle in radians |
Common math.h Mistakes
| Mistake | Correct Idea |
|---|---|
Using sin(90) and expecting sine of 90 degrees. |
Convert degrees to radians first. |
Assuming M_PI exists everywhere. |
Do not rely on it for portable C17 code. |
Using abs() for a double. |
Use fabs() for floating-point values. |
| Forgetting the math library during some GCC builds. | Add -lm when your platform requires it. |
- Which header declares
sqrt()? - Do
sin()andcos()use degrees or radians? - Which function gives the absolute value of a
double?
Practice Answers
1. Which header declares sqrt()?
Use <math.h>.
2. Degrees or radians?
They use radians.
3. Which floating-point absolute-value function?
Use fabs().
FAQs
What is math.h in C?
math.h is a standard C header that declares mathematical functions such as sqrt(), pow(), sin(), cos(), fabs(), floor(), and ceil().
Do sin() and cos() use degrees in C?
No. The trigonometric functions in math.h use angles measured in radians.
Is M_PI guaranteed by standard C?
No. M_PI is a common extension, but it is not guaranteed by the C17 standard.
Why might GCC need -lm for math functions?
On many Unix-like systems, mathematical functions are supplied by the math library, so a GCC link command may need -lm.
Summary
<math.h> provides standard mathematical functions for roots, powers, rounding, trigonometry, absolute values, and more. Common functions include sqrt(), pow(), fabs(), floor(), ceil(), sin(), and cos(). Remember that trigonometric functions use radians and that M_PI is not guaranteed by standard C17.