<math.h> is the C standard header for real floating-point math: trigonometry, powers, roots, logs, rounding, and helpers like fabs. Most functions take and return double. On GCC, link with -lm.
Remember
#include <math.h>
sin / cos / tan → radians (not degrees)
sqrt / pow → roots and powers
fabs → |x| for doubles
gcc file.c -std=c11 -lm -o out
bad domain (e.g. sqrt(-1)) → NaN / EDOM
Include the header, convert degrees if needed, call the function, and link libm.
Add #include <math.h> (and <stdio.h> if you print).
For trig, convert degrees → radians: deg * M_PI / 180.0 (define M_PI if missing).
Call the function with double arguments and store the result.
Compile with gcc file.c -std=c11 -lm -o out on GCC/Clang.
Piece
Required?
Role
#include <math.h>
Yes
Declares math functions and macros
-lm
Usually (GCC)
Links the math library (libm)
Radians conversion
For trig from degrees
deg * π / 180 before sin/cos
Pseudocode
Pseudocode
include math.h
link with -lm
x = some double value
if using trig from degrees:
rad = degrees * PI / 180
y = sin(rad) // or cos / tan
else if root:
if x < 0: handle error // sqrt domain
else: y = sqrt(x)
else:
y = pow(base, exp) // or fabs, floor, …
print or use y
Reference
Cheat Sheet
Keep these facts handy while writing math code.
Topic
Detail
Header
#include <math.h>
Default type
double (sinf/sqrtf for float)
Trig units
Radians only
Degrees → rad
deg * M_PI / 180.0
Link (GCC)
gcc … -lm
Core calls
sqrt, pow, sin, cos, fabs, hypot
Sample check
hypot(3, 4) → 5; sin(π/6) → 0.5
Try it
Live Preview
Simulate common math.h calls in the browser. Same ideas as the C examples below (compile those with -lm).
Browser simulation of math.h results (same idea as the C examples below).
GCC reports undefined references to sqrt, pow, etc. Add -lm at the end of the link line.
domain
Negative sqrt / bad log
Invalid domains return NaN and may set errno to EDOM. Validate user input before calling.
pi
M_PI is not ISO C
Provide a fallback #define, or use acos(-1.0), if your compiler is strict.
Analysis
Performance Notes
Approach
Cost
Best for
sqrt / fabs
Very cheap (often hardware)
Everyday numeric code
sin / pow / log
Moderate per call
Formulas; avoid needless repeats in hot loops
float variants (sinf)
Often faster / less memory
Large arrays when precision allows
libm is highly optimized. Prefer clear calls over hand-rolled approximations unless profiling shows a hotspot. See also float.h for precision limits.
Remember
Key Takeaways
Role: standard real math for C (double by default).
Link: use -lm with GCC/Clang.
Trig: radians only—convert degrees first.
Safety: validate domains; check errno when needed.
One line:math.h + -lm gives you portable trig, roots, and powers—just remember radians.
Frequently Asked Questions
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.