C programs use the standard math library for roots, powers, trigonometry, logarithms, and rounding. This guide teaches how to include <math.h>, link correctly, work with double values, and call essential functions before diving into the full tutorial series.
01
math.h
Standard header.
02
Link -lm
GCC/Clang tip.
03
double
Default type.
04
Radians
Trig angles.
05
Roots & Pow
sqrt, pow.
06
30 Functions
Full index.
Fundamentals
Definition and Usage
C does not have a built-in x^2 operator or a Math class like Java. Instead, you call library functions declared in <math.h>—sqrt(x), pow(x, y), sin(angle), and many more. Most take and return double for floating-point precision.
Integer math uses normal operators (+, -, *, /, %). When you need square roots, trigonometry, or careful rounding of decimals, switch to math.h functions. Integer absolute value abs() is in <stdlib.h> instead.
💡
Beginner Tip
Always compile with -lm on GCC/Clang when using math functions: gcc main.c -std=c11 -o main -lm. Circular trig (sin, cos, tan) expects radians—multiply degrees by M_PI / 180.0 first.
Foundation
📝 Syntax
Include the header, call functions, and link the math library:
C
#include <math.h>
#include <stdio.h>
int main(void) {
double x = 16.0;
double root = sqrt(x);
double power = pow(2.0, 10.0);
printf("sqrt(16) = %.0f\n", root);
printf("2^10 = %.0f\n", power);
return 0;
}
/* gcc main.c -std=c11 -o main -lm */
Common Headers
<math.h> — floating-point math: sqrt, sin, floor, fabs, etc.
<stdlib.h> — integer abs(), labs(), llabs().
_USE_MATH_DEFINES — define before <math.h> on MSVC for M_PI.
Type Variants
sqrt(x) /* double in, double out */
sqrtf(x) /* float in, float out */
sqrtl(x) /* long double in, long double out */
Cheat Sheet
⚡ Quick Reference
Square root
sqrt(25.0)
Returns 5.0
Power
pow(2.0, 8.0)
Returns 256.0
Degrees → rad
deg * M_PI / 180.0
Before sin/cos
Compile
gcc -std=c11 -lm
Link math lib
Hands-On
Examples Gallery
Compile every example with gcc file.c -std=c11 -o out -lm. These five programs cover setup, absolute value, trigonometry, powers, and rounding.
📚 Getting Started
Include math.h and call your first function.
Example 1 — Include math.h and Call sqrt()
The smallest working program that uses the math library.
On negatives, floor and trunc diverge. round picks the nearest whole number (halfway cases tie away from zero). Choose based on your business logic.
Applications
🚀 Common Use Cases
Graphics and games — angles with sin/cos, distances with sqrt or hypot.
Science and engineering — exponentials with exp, logarithms with log/log10.
Finance — compound interest with pow, rounding with floor/round.
Signal processing — hyperbolic functions sinh, cosh, tanh.
Grid and UI layout — snap coordinates with floor, ceil, or trunc.
Error checking — compare floats with tolerance using fabs(a - b) < epsilon.
🧠 How C Math Works
1
Include math.h
Add #include <math.h> and link with -lm on GCC/Clang.
Setup
2
Pass double values
Most functions take double; integers promote automatically in calls.
Input
3
Library computes result
Platform math library handles precision, edge cases, and special values.
Compute
=
🔢
Use the return value
Assign to a variable, print with printf, or pass to the next calculation.
Important
📝 Notes
Link with -lm on GCC/Clang when using math.h functions.
Trig functions use radians, not degrees—convert explicitly.
abs() is for integers; fabs() is for double.
Floating-point results may have tiny rounding errors—avoid == for exact equality.
pow(0, 0) and domain errors may return NaN or trigger errno—check docs per function.
Hyperbolic functions (sinh, tanh) are not the same as circular trig despite similar names.
Wrap Up
Conclusion
C math through <math.h> gives you roots, powers, trigonometry, logarithms, rounding, and more. Remember -lm, radians for trig, and the difference between integer abs and floating fabs.
Practice the examples on this page, then explore the full Math Functions index for in-depth tutorials on all 30 functions in our series.
Use hypot(x, y) instead of sqrt(x*x + y*y) for long sides
❌ Don’t
Pass degrees directly to sin() without conversion
Use abs() on double values (use fabs)
Compare floats with == after math operations
Forget -lm and wonder why linking fails
Confuse tan() with tanh()
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about C math
Use these points as you write your first C programs.
5
Core concepts
🔢01
math.h
Standard lib.
Basics
📝02
Link -lm
GCC/Clang.
Compile
📈03
Radians
Not degrees.
Trig
🛠04
fabs
Not abs.
Types
📚05
30 Functions
Full index.
Next step
❓ 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.
Did you know?
C has no exponentiation operator—you cannot write 2^10 in standard C. Use pow(2.0, 10.0) from <math.h> instead, or bit shifts for powers of two: 1 << 10 equals 1024 for integers.