Chapter 29Beginner

C math.h Library Reference

Complete reference for math.h - power, roots, trigonometry, logarithms, and rounding functions.

15 min readUpdated 2024-12-16
math.hpowsqrtsincoslogceilfloor

What You Will Learn

  • ✓Use power and root functions
  • ✓Apply trigonometric functions
  • ✓Work with logarithms
  • ✓Round numbers correctly

01Introduction to math.h

📐 What is math.h?

<math.h> provides mathematical functions for trigonometry, logarithms, powers, rounding, and more.

⚠️ Link Math Library

On Linux/GCC, you must link with -lm:
gcc program.c -o program -lm

CategoryFunctions
Power/Rootpow, sqrt, cbrt, hypot
Trigonometrysin, cos, tan, asin, acos, atan
Logarithmlog, log10, log2, exp
Roundingceil, floor, round, trunc
Otherfabs, fmod, fmax, fmin

02Power and Root Functions

FunctionPrototypeDescription
pow()double pow(double base, double exp)base raised to exp (x^y)
sqrt()double sqrt(double x)Square root (√x)
cbrt()double cbrt(double x)Cube root (∛x)
hypot()double hypot(double x, double y)Hypotenuse √(x²+y²)
power_root.c
C
1#include <stdio.h>
2#include <math.h>
3
4int main() {
5 // Power
6 printf("2^10 = %.0f\n", pow(2, 10)); // 1024
7 printf("5^3 = %.0f\n", pow(5, 3)); // 125
8
9 // Square root
10 printf("√16 = %.0f\n", sqrt(16)); // 4
11 printf("√2 = %.4f\n", sqrt(2)); // 1.4142
12
13 // Cube root
14 printf("∛27 = %.0f\n", cbrt(27)); // 3
15
16 // Hypotenuse (Pythagorean theorem)
17 printf("hypot(3,4) = %.0f\n", hypot(3, 4)); // 5
18
19 return 0;
20}

03Trigonometric Functions

💡 Radians, Not Degrees!

All trig functions use radians. Convert: radians = degrees × (π / 180)

FunctionPrototypeDescription
sin()double sin(double x)Sine (x in radians)
cos()double cos(double x)Cosine
tan()double tan(double x)Tangent
asin()double asin(double x)Arc sine (inverse)
acos()double acos(double x)Arc cosine
atan()double atan(double x)Arc tangent
atan2()double atan2(double y, double x)Arc tangent of y/x
trig_functions.c
C
1#include <stdio.h>
2#include <math.h>
3
4#define PI 3.14159265358979323846
5
6int main() {
7 // Convert degrees to radians
8 double deg = 45.0;
9 double rad = deg * (PI / 180.0);
10
11 printf("sin(45°) = %.4f\n", sin(rad)); // 0.7071
12 printf("cos(45°) = %.4f\n", cos(rad)); // 0.7071
13 printf("tan(45°) = %.4f\n", tan(rad)); // 1.0000
14
15 // Inverse trig (returns radians)
16 double angle = asin(0.5); // 30° in radians
17 printf("asin(0.5) = %.4f rad = %.1f°\n",
18 angle, angle * (180.0 / PI));
19
20 // Use M_PI constant (if available)
21 printf("sin(π/2) = %.4f\n", sin(M_PI / 2)); // 1.0000
22
23 return 0;
24}

04Exponential and Logarithmic

FunctionPrototypeDescription
exp()double exp(double x)e^x (e = 2.718...)
log()double log(double x)Natural log (ln x)
log10()double log10(double x)Base-10 log
log2()double log2(double x)Base-2 log
log_functions.c
C
1#include <stdio.h>
2#include <math.h>
3
4int main() {
5 // Exponential
6 printf("e^1 = %.4f\n", exp(1)); // 2.7183
7 printf("e^2 = %.4f\n", exp(2)); // 7.3891
8
9 // Natural logarithm (base e)
10 printf("ln(e) = %.4f\n", log(M_E)); // 1.0000
11 printf("ln(10) = %.4f\n", log(10)); // 2.3026
12
13 // Base-10 logarithm
14 printf("log10(100) = %.4f\n", log10(100)); // 2.0000
15 printf("log10(1000) = %.4f\n", log10(1000)); // 3.0000
16
17 // Base-2 logarithm
18 printf("log2(8) = %.4f\n", log2(8)); // 3.0000
19 printf("log2(1024) = %.4f\n", log2(1024)); // 10.0000
20
21 return 0;
22}

05Rounding Functions

FunctionPrototypeDescription
ceil()double ceil(double x)Round up ⌈x⌉
floor()double floor(double x)Round down ⌊x⌋
round()double round(double x)Round to nearest
trunc()double trunc(double x)Truncate (toward 0)
rounding.c
C
1#include <stdio.h>
2#include <math.h>
3
4int main() {
5 double x = 3.7, y = -3.7;
6
7 printf("Value ceil floor round trunc\n");
8 printf("%.1f %.0f %.0f %.0f %.0f\n",
9 x, ceil(x), floor(x), round(x), trunc(x));
10 printf("%.1f %.0f %.0f %.0f %.0f\n",
11 y, ceil(y), floor(y), round(y), trunc(y));
12
13 // Output:
14 // Value ceil floor round trunc
15 // 3.7 4 3 4 3
16 // -3.7 -3 -4 -4 -3
17
18 return 0;
19}
Output

Value ceil floor round trunc

3.7 4 3 4 3

-3.7 -3 -4 -4 -3

06Other Useful Functions

FunctionPrototypeDescription
fabs()double fabs(double x)Absolute value |x|
fmod()double fmod(double x, double y)Floating-point modulo
fmax()double fmax(double x, double y)Maximum of x, y
fmin()double fmin(double x, double y)Minimum of x, y
other_math.c
C
1#include <stdio.h>
2#include <math.h>
3
4int main() {
5 // Absolute value
6 printf("fabs(-5.5) = %.1f\n", fabs(-5.5)); // 5.5
7
8 // Floating-point modulo
9 printf("fmod(5.3, 2) = %.1f\n", fmod(5.3, 2)); // 1.3
10
11 // Max and Min
12 printf("fmax(3.5, 2.1) = %.1f\n", fmax(3.5, 2.1)); // 3.5
13 printf("fmin(3.5, 2.1) = %.1f\n", fmin(3.5, 2.1)); // 2.1
14
15 return 0;
16}

07Math Constants

ConstantValueDescription
M_PI3.14159...π (pi)
M_E2.71828...e (Euler's number)
M_SQRT21.41421...√2
M_LN20.69314...ln(2)

💡 Note

These constants may require #define _USE_MATH_DEFINESbefore including math.h on some compilers (MSVC).

08Summary

🎯 Key Functions

Power/Root:

pow, sqrt, cbrt, hypot

Trig:

sin, cos, tan, asin, acos, atan

Log/Exp:

log, log10, log2, exp

Rounding:

ceil, floor, round, trunc

09Next Steps

Learn about character classification functions: