C has no built-in x^2 operator or Math class like some languages. For roots, powers, trigonometry, logs, and rounding you call library functions from <math.h>—for example sqrt(x), pow(x, y), and sin(angle).
Operators vs math.h
Everyday arithmetic uses operators: +, -, *, /, %. When you need square roots, angles, or careful decimal rounding, switch to math.h. Integer absolute value abs() lives in <stdlib.h> instead.
What should beginners learn first?
Include <math.h>, compile with -lm on GCC/Clang, then practice fabs(), sqrt(), pow(), and sin() with radian conversion. Those cover absolute value, roots, powers, and basic trigonometry.
This lesson vs the Functions index
This page is the C math lesson: setup, radians, examples, and pitfalls. The Math Functions index is only a catalog—search and open individual guides such as hypot(). Learn here first, then browse the index.
Link with -lm.
On GCC/Clang: gcc main.c -std=c11 -o main -lm. Without -lm you often see undefined reference to sqrt.
Trig uses radians.
sin, cos, and tan do not take degrees. Convert first: rad = deg * M_PI / 180.0.
Default type is double.
Most math.h functions take and return double. Use f / l suffixes for float / long double variants.
Syntax
C Syntax
Include the header, call functions, and link the math library:
Test negatives when choosing a rounding rule for your program.
Next
Where to Go Next
This page teaches how math works in C (math.h, -lm, radians, examples). The full searchable catalog of every function tutorial lives on the Math Functions index—start with these six:
Most C math bugs come from missing -lm, degrees vs radians, or the wrong absolute-value function.
-lm
undefined reference to sqrt
Add -lm at the end of your GCC/Clang link line. Including the header is not enough.
rad
Passing degrees to sin()
sin(45) is 45 radians. Convert with deg * M_PI / 180.0 first.
abs
abs vs fabs mix-up
Use abs for int and fabs for double. Wrong choice causes warnings or wrong results.
√
sqrt of a negative
Real sqrt has a domain error for negatives. Check the sign or use complex math intentionally.
rnd
floor vs trunc on negatives
For -2.7, floor gives -3 and trunc gives -2. Pick the rule your logic needs.
≈
Comparing doubles with ==
Prefer a tolerance: fabs(a - b) < 1e-9 (or your domain epsilon).
Platform
Platform Support
<math.h> is part of standard C. Linking details differ by toolchain.
Toolchain
Support
Notes
GCC (Linux / MinGW)
Full
Usually needs -lm
Clang (macOS / Linux)
Full
Usually needs -lm
MSVC
Full
Math often links automatically; define _USE_MATH_DEFINES for M_PI
Embedded / freestanding
Varies
May need a vendor math library or soft-float support
Core functions such as sqrt, pow, sin, fabs, and floor are portable C99/C11 APIs. Some constants (like M_PI) are common extensions—guard them or define your own PI when portability matters.
Remember
Key Takeaways
math.h provides floating-point math; link with -lm on GCC/Clang.
Start withfabs, sqrt, pow, and sin (radians).
Default type is double; use f/l variants when needed.
One line: C math is math.h + -lm—use double, convert degrees to radians, then open the functions index for details.
Frequently Asked Questions
math.h is the standard header for floating-point math: sqrt, pow, sin, cos, log, floor, fabs, and dozens more. Include it with #include <math.h> and link with -lm on GCC/Clang: gcc program.c -o program -lm.
On GCC and Clang, math functions live in libm (the math library), separate from libc. Without -lm you get linker errors like undefined reference to sqrt. MSVC often links math automatically.
Radians. Convert degrees first: double rad = degrees * (M_PI / 180.0); then sin(rad). On MSVC define _USE_MATH_DEFINES before math.h to get M_PI.
abs() works on integers and is declared in stdlib.h. fabs() works on double floating-point values and is in math.h. Use the one matching your variable type.
Most math.h functions take and return double. Float variants end in f (sqrtf, sinf); long double variants end in l (sqrtl, sinl). Match the variant to your variable type for clarity.
Learn math.h setup and -lm on this page, then open the Math Functions index for fabs(), sqrt(), pow(), and sin(). Those four cover absolute value, roots, powers, and basic trigonometry.