Skip to content
StudyFame

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() and round().
  • Why abs() is in stdlib.h, not math.h.
  • The -lm flag 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.

FunctionDoesExampleResult
sqrt(x)Square rootsqrt(25)5.0
pow(x, y)x to the power ypow(2, 10)1024.0
ceil(x)Round up to the next whole numberceil(4.2)5.0
floor(x)Round downfloor(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 decimalfabs(-7.5)7.5
fmod(x, y)Remainder for decimalsfmod(10, 3)1.0
log10(x) / log(x)Log base 10 / natural loglog10(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

  1. All math.h functions return a double, so we print them with %.2f.
  2. ceil(4.2) goes up to 5; floor(4.8) goes down to 4, even though 4.8 is close to 5.
  3. round(2.5) gives 3: exact halves are rounded away from zero.
  4. abs(-12) comes from stdlib.h and works on int, so it is printed with %d.

How Library Maths Functions Work

  • math.h only 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 to double: 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 in stdlib.h; fabs() (double) is in math.h.
  • Add -lm with GCC on Linux.
  • Trigonometric functions use radians, not degrees.

Common Mistakes Beginners Make

MistakeWhat happensFix
Missing #include <math.h>Wrong results or errorsInclude the header
undefined reference to 'sqrt'Linker can't find the maths libraryAdd -lm
Printing sqrt() with %dGarbage outputUse %f
sin(90) expecting 190 is taken as radianssin(90 * 3.14159265 / 180)
Using ^ for powerXOR, not powerpow(x, y)

math.h Functions Practice Questions

  1. Which header file contains sqrt() and pow()?
  2. What is the difference between ceil() and floor()? Give an example.
  3. What do ceil(-2.3) and floor(-2.3) return?
  4. Why is -lm needed with GCC on Linux?
  5. Which function gives the absolute value of an int, and where is it declared?
Show answers
  1. math.h
  2. ceil rounds up, floor rounds down. ceil(4.2) = 5, floor(4.8) = 4.
  3. ceil(−2.3) = −2 (up towards zero), floor(−2.3) = −3 (down).
  4. Because the maths functions are in a separate library (libm) that must be linked.
  5. abs(), declared in stdlib.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)

  1. ceil(7.1) returns:

    A) 7.0   B) 8.0   C) 7.1   D) 7

  2. floor(7.9) returns:

    A) 7.0   B) 8.0   C) 7.9   D) 8

  3. pow(3, 4) returns:

    A) 12   B) 64   C) 81.0   D) 7

  4. Return type of most math.h functions:

    A) int   B) float   C) double   D) char

  5. Which flag links the maths library with GCC on Linux?

    A) -math   B) -lm   C) -m   D) -lmath.h

Show answers
  1. B: 8.0.
  2. A: 7.0.
  3. C: 81.0.
  4. C: double.
  5. B: -lm

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

Headermath.h (return type double)
sqrt / powSquare root / power
ceil / floor / roundUp / down / nearest
fabs / fmodDecimal absolute value / remainder
abs()Integer absolute value, in stdlib.h
Linux GCCAdd -lm
Trig functionsUse 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 -lm to 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.