Math Functions in C (math.h)
Square roots, powers, rounding... you don't need to write these yourself. C gives you ready-made maths functions in the header file math.h.
In this lesson you will learn the most used maths functions with their output, a common compile error (and its fix), and a classic program: roots of a quadratic equation.
What You Will Learn
- The most important functions in
math.h. - The difference between
ceil(),floor()andround(). - Why
abs()is instdlib.h, notmath.h. - The
-lmflag for GCC on Linux. - Roots of a quadratic equation program.
Math Functions in C (math.h)
Exam definition: math.h is a standard header file that declares mathematical library functions such as sqrt(), pow(), ceil(), floor() and trigonometric functions. Most of them take and return double values.
| Function | Does | Example | Result |
|---|---|---|---|
sqrt(x) | Square root | sqrt(25) | 5.0 |
pow(x, y) | x to the power y | pow(2, 10) | 1024.0 |
ceil(x) | Round up to the next whole number | ceil(4.2) | 5.0 |
floor(x) | Round down | floor(4.8) | 4.0 |
round(x) | Round to nearest (halves go away from 0) | round(2.5) | 3.0 |
fabs(x) | Absolute value of a decimal | fabs(-7.5) | 7.5 |
fmod(x, y) | Remainder for decimals | fmod(10, 3) | 1.0 |
log10(x) / log(x) | Log base 10 / natural log | log10(1000) | 3.0 |
sin(x), cos(x), tan(x) | Trigonometry (x in radians) | sin(0) | 0.0 |
abs(x) for integers is in stdlib.h, not math.h. Use fabs() for decimals.
Memory trick: ceiling is up (the ceiling is above you), floor is down (the floor is below you). fabs and fmod have an f for floating-point (decimals).
The -lm flag (GCC on Linux)
On Linux, maths functions live in a separate library. If you get undefined reference to 'sqrt', add -lm at the end of the command:
gcc program.c -o program -lm
On Windows (MinGW) and macOS this is usually not needed, but adding it does no harm.
Syntax: Using math.h
#include <math.h>
double result = sqrt(number);
double p = pow(base, exponent);
Print the results with %f (or %.2f), because they are double values.
Example 1: The Most Used math.h Functions
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(void)
{
printf("sqrt(25) = %.2f\n", sqrt(25));
printf("pow(2, 10) = %.2f\n", pow(2, 10));
printf("ceil(4.2) = %.2f\n", ceil(4.2));
printf("floor(4.8) = %.2f\n", floor(4.8));
printf("round(2.5) = %.2f\n", round(2.5));
printf("fabs(-7.5) = %.2f\n", fabs(-7.5));
printf("fmod(10, 3) = %.2f\n", fmod(10, 3));
printf("log10(1000) = %.2f\n", log10(1000));
printf("abs(-12) = %d\n", abs(-12));
return 0;
}
Output
sqrt(25) = 5.00 pow(2, 10) = 1024.00 ceil(4.2) = 5.00 floor(4.8) = 4.00 round(2.5) = 3.00 fabs(-7.5) = 7.50 fmod(10, 3) = 1.00 log10(1000) = 3.00 abs(-12) = 12
Code Explanation
- All
math.hfunctions return adouble, so we print them with%.2f. ceil(4.2)goes up to 5;floor(4.8)goes down to 4, even though 4.8 is close to 5.round(2.5)gives 3: exact halves are rounded away from zero.abs(-12)comes fromstdlib.hand works onint, so it is printed with%d.
How Library Maths Functions Work
math.honly declares the functions (their prototypes), so the compiler knows their types.- The actual code is in the maths library, which the linker joins with your program. That's why Linux needs
-lm. - If you pass an
int, it is automatically converted todouble:sqrt(25)works. - For impossible inputs, like
sqrt(-1), the result is NaN ("not a number").
Example 2: Roots of a Quadratic Equation in C
For ax² + bx + c = 0, the roots are (−b ± √(b² − 4ac)) / 2a.
#include <stdio.h>
#include <math.h>
int main(void)
{
double a, b, c;
printf("Enter a, b, c: ");
scanf("%lf %lf %lf", &a, &b, &c);
double d = b * b - 4 * a * c; /* discriminant */
if (d > 0)
{
double r1 = (-b + sqrt(d)) / (2 * a);
double r2 = (-b - sqrt(d)) / (2 * a);
printf("Two real roots: %.2f and %.2f\n", r1, r2);
}
else if (d == 0)
printf("One real root: %.2f\n", -b / (2 * a));
else
printf("Roots are imaginary\n");
return 0;
}
Output
Enter a, b, c: 1 -5 6 Two real roots: 3.00 and 2.00
For x² − 5x + 6: d = 25 − 24 = 1, √1 = 1, so the roots are (5 + 1)/2 = 3 and (5 − 1)/2 = 2.
Important Points to Remember
- Include
math.h; most functions use double. - ceil = up, floor = down, round = nearest.
abs()(int) is instdlib.h;fabs()(double) is inmath.h.- Add
-lmwith GCC on Linux. - Trigonometric functions use radians, not degrees.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
Missing #include <math.h> | Wrong results or errors | Include the header |
undefined reference to 'sqrt' | Linker can't find the maths library | Add -lm |
Printing sqrt() with %d | Garbage output | Use %f |
sin(90) expecting 1 | 90 is taken as radians | sin(90 * 3.14159265 / 180) |
Using ^ for power | XOR, not power | pow(x, y) |
math.h Functions Practice Questions
- Which header file contains
sqrt()andpow()? - What is the difference between
ceil()andfloor()? Give an example. - What do
ceil(-2.3)andfloor(-2.3)return? - Why is
-lmneeded with GCC on Linux? - Which function gives the absolute value of an int, and where is it declared?
Show answers
math.h- ceil rounds up, floor rounds down. ceil(4.2) = 5, floor(4.8) = 4.
- ceil(−2.3) = −2 (up towards zero), floor(−2.3) = −3 (down).
- Because the maths functions are in a separate library (libm) that must be linked.
abs(), declared instdlib.h.
Coding Exercise
Find the length of the hypotenuse of a right triangle with sides 3 and 4 using sqrt() and pow(). (Answer: 5.)
Show solution
#include <stdio.h>
#include <math.h>
int main(void)
{
double a = 3, b = 4;
double c = sqrt(pow(a, 2) + pow(b, 2));
printf("Hypotenuse = %.2f\n", c);
return 0;
}
Hypotenuse = 5.00
Quick Quiz: math.h Functions in C (MCQ)
-
ceil(7.1)returns:A) 7.0 B) 8.0 C) 7.1 D) 7
-
floor(7.9)returns:A) 7.0 B) 8.0 C) 7.9 D) 8
-
pow(3, 4)returns:A) 12 B) 64 C) 81.0 D) 7
-
Return type of most math.h functions:
A) int B) float C) double D) char
-
Which flag links the maths library with GCC on Linux?
A)
-mathB)-lmC)-mD)-lmath.h
Show answers
- B: 8.0.
- A: 7.0.
- C: 81.0.
- C: double.
- B:
-lm
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Header | math.h (return type double) |
|---|---|
| sqrt / pow | Square root / power |
| ceil / floor / round | Up / down / nearest |
| fabs / fmod | Decimal absolute value / remainder |
| abs() | Integer absolute value, in stdlib.h |
| Linux GCC | Add -lm |
| Trig functions | Use radians |
Frequently asked questions
- What is the difference between ceil() and floor()?
- ceil rounds up to the next whole number; floor rounds down.
- Why do we get "undefined reference to sqrt"?
- The maths library isn't linked; add
-lmto the gcc command. - What is the difference between abs() and fabs()?
- abs() is for int (stdlib.h); fabs() is for double (math.h).
- How do you find x to the power y in C?
- With
pow(x, y), not^.
Remember
- Ceiling up, floor down.
- fabs, fmod = floating point.
Next Topic
Next lesson: Arrays in C. Module 08 starts with arrays: storing many values under one name.