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.

Include math.h

#include <math.h>

The header declares standard mathematical functions and related facilities.

What math.h provides in C The math.h header branches into roots and powers, rounding functions, trigonometric functions, and absolute-value functions. math.h → MATHEMATICAL FUNCTIONS STANDARD HEADER <math.h> ROOTS / POWERS sqrt() pow() ROUNDING floor() ceil() TRIGONOMETRY sin() cos() ABSOLUTE VALUE fabs() IMPORTANT Trigonometric functions use radians, not degrees. M_PI is common on some systems, but it is not guaranteed by standard C17.

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>.

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

FunctionPurpose
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

MistakeCorrect 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.
  1. Which header declares sqrt()?
  2. Do sin() and cos() use degrees or radians?
  3. 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.