The <math.h> header is the standard math library for C: trigonometry, powers, roots, logarithms, rounding, and more. Most functions take and return double. You include the header and link with -lm on GCC—a step the old reference omitted from its compile instructions.
01
sqrt pow
Roots/power.
02
sin cos
Trig (rad).
03
log exp
Logs/exp.
04
fabs
Abs value.
05
-lm
Link flag.
06
double
Main type.
Fundamentals
Definition and Usage
<math.h> declares functions declared in the C standard for real floating-point math. They operate on double by default; float variants end in f (sinf, sqrtf) and long double variants end in l (sinl, sqrtl).
Trigonometric functions expect radians, not degrees. Invalid inputs (like sqrt(-1)) can return NaN and set errno to EDOM. Huge results may set ERANGE.
💡
Beginner Tip
Compile math programs with gcc program.c -std=c11 -lm -o program. Without -lm, GCC reports undefined references to sqrt, pow, etc.
Foundation
📝 Syntax
Include the header:
C
#include <math.h>
Common functions by category
Trigonometry — sin, cos, tan, asin, acos, atan, atan2 (radians).
Embedded sensors — sqrtf/sinf float variants for speed.
🧠 How math.h Works
1
Include & link
#include <math.h> and -lm on GCC.
Setup
2
Call function
Pass double arguments; convert degrees to radians for trig.
Compute
3
Get double result
Store in double; watch for NaN on invalid input.
Result
=
💬
Standard math in C
Portable, optimized routines instead of hand-written approximations.
Important
📝 Notes
Trig functions use radians; convert from degrees with * M_PI / 180.0.
Link with -lm on GCC/Clang (not always needed on MSVC).
Most functions are defined for double; use sinf for float.
Domain errors set errno to EDOM; range errors may set ERANGE.
M_PI is not in strict ISO C—provide a fallback #define if needed.
For integer absolute value, use abs from <stdlib.h>, not fabs.
Performance
⚡ Optimization
libm functions are highly optimized on modern CPUs. Use float variants (sqrtf, sinf) in hot loops with large arrays when precision allows. Avoid calling heavy functions inside tight inner loops when a lookup table or approximation suffices—but for most apps, math.h is plenty fast.
Wrap Up
Conclusion
<math.h> is the standard way to do real-number math in C: powers, roots, trig, logs, and rounding. Include the header, link with -lm, pass radians to trig functions, and handle edge cases with errno or input checks.
Explore the C Math Functions hub for dedicated tutorials on sqrt, sin, pow, and every other function in the library.
math.h declares standard mathematical functions: trigonometry (sin, cos, tan), roots and powers (sqrt, pow), logarithms (log, log10), exponentials (exp), rounding (floor, ceil), and utilities like fabs and fmod. Most functions work on double values.
On GCC and many Unix-like toolchains, math functions live in a separate libm library. Compile with gcc program.c -std=c11 -lm -o program. MSVC links math automatically; MinGW/GCC on Windows usually still needs -lm.
Radians. Convert degrees first: radians = degrees * M_PI / 180.0. Forgetting this is the most common beginner mistake with C trigonometry.
M_PI is a common macro for pi (~3.14159). It is not in strict ISO C math.h but is available on many systems (GNU, BSD). Portable alternatives: acos(-1.0) or define your own constant.
sqrt of a negative number returns NaN and may set errno to EDOM (domain error). Check inputs when possible; use fabs or validate before sqrt. See errno.h for error reporting.
This page introduces the header. CodeToFun also has detailed per-function tutorials at /c/math/function (abs, sin, sqrt, pow, and more). Start here for overview and linking; dive into individual functions there.
Did you know?
Before math.h, programmers wrote their own Taylor-series approximations for sine and cosine. Today’s libm uses carefully tuned hardware and software implementations—often one CPU instruction for sqrt on x86. That is why linking -lm pulls in decades of numerical expertise for free.