C math.h Header

Beginner
7 min read
Updated: Oct 2026
3 examples
Live preview

What Is math.h?

<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
UsePrefer
Square root / powersqrt / pow
Sine / cosine (radians)sin / cos
Domain / range errorserrno (EDOM/ERANGE)

How to Use math.h in C

Include the header, convert degrees if needed, call the function, and link libm.

  1. Add #include <math.h> (and <stdio.h> if you print).
  2. For trig, convert degrees → radians: deg * M_PI / 180.0 (define M_PI if missing).
  3. Call the function with double arguments and store the result.
  4. Compile with gcc file.c -std=c11 -lm -o out on GCC/Clang.
PieceRequired?Role
#include <math.h>YesDeclares math functions and macros
-lmUsually (GCC)Links the math library (libm)
Radians conversionFor trig from degreesdeg * π / 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

Cheat Sheet

Keep these facts handy while writing math code.

TopicDetail
Header#include <math.h>
Default typedouble (sinf/sqrtf for float)
Trig unitsRadians only
Degrees → raddeg * M_PI / 180.0
Link (GCC)gcc … -lm
Core callssqrt, pow, sin, cos, fabs, hypot
Sample checkhypot(3, 4) → 5; sin(π/6) → 0.5

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).

Result
hypot(3, 4)
→ √(3² + 4²)
→ 5.000000

/* classic 3-4-5 triangle */

Worked Walkthrough — 3-4-5 Distance

Find the distance from (0, 0) to (3, 4) with hypot, then check sin(30°) after converting to radians.

Calculation
Distance:
  hypot(3, 4) = √(9 + 16) = √25 = 5

Trig (degrees → radians first):
  rad = 30 * π / 180 ≈ 0.5236
  sin(rad) ≈ 0.5
  cos(rad) ≈ 0.866

Check: sin(30°) should be 0.5 ✓
Never pass 30 directly to sin — that is 30 radians!

C Examples

Compile every example with gcc file.c -std=c11 -lm -o out. See the C Math Functions index for per-function deep dives.

Example 1 — pow, sqrt, and fabs

A short tour of everyday pow, sqrt, and fabs.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double base = 2.0;
    double expn = 8.0;

    printf("pow(%.1f, %.1f) = %.2f\n", base, expn, pow(base, expn));
    printf("sqrt(%.1f) = %.2f\n", expn, sqrt(expn));
    printf("fabs(-%.1f) = %.2f\n", base, fabs(-base));

    return 0;
}

How It Works

  1. Include headers. math.h for the functions, stdio.h for printf.
  2. Power. pow(2, 8) means 28 → 256.
  3. Root. sqrt(8) is √8 ≈ 2.83 (not an integer).
  4. Absolute value. fabs drops the sign of a double. Use abs from stdlib.h for integers.

Example 2 — Convert Degrees Before sin/cos

Trig functions expect radians. Convert 30° first, then call sin.

C
#include <math.h>
#include <stdio.h>

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

double deg_to_rad(double degrees) {
    return degrees * M_PI / 180.0;
}

int main(void) {
    double deg = 30.0;
    double rad = deg_to_rad(deg);

    printf("%.0f degrees = %.4f radians\n", deg, rad);
    printf("sin(30 deg) = %.4f\n", sin(rad));
    printf("cos(30 deg) = %.4f\n", cos(rad));

    return 0;
}

How It Works

  1. Define π if needed. M_PI is common but not strict ISO C—the #ifndef fallback keeps the program portable.
  2. Convert once. deg_to_rad(30) → about 0.5236 radians.
  3. Call trig on radians. sin(rad) ≈ 0.5, cos(rad) ≈ 0.866.
  4. Avoid the trap. sin(30) without conversion means 30 radians—not 30 degrees.

Example 3 — Distance with hypot

Safer than sqrt(a*a + b*b) for large values—avoids overflow. Classic 3-4-5 triangle.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double dx = 3.0;
    double dy = 4.0;

    printf("Distance from (0,0) to (%.0f,%.0f) = %.1f\n",
           dx, dy, hypot(dx, dy));

    return 0;
}

How It Works

  1. Pick two legs. dx = 3 and dy = 4 are the sides of a right triangle.
  2. Call hypot. hypot(dx, dy) computes √(dx² + dy²) with better numerical stability.
  3. Get the hypotenuse. Result is 5.0—the classic 3-4-5 check.
  4. Prefer over manual squares. For huge coordinates, a*a + b*b can overflow before sqrt; hypot is designed for this.

Edge Cases & Pitfalls

Check these before relying on math.h in real projects.

units

Trig uses radians

Passing degrees to sin/cos silently gives wrong answers. Always convert first.

link

Forgot -lm

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.

Performance Notes

ApproachCostBest for
sqrt / fabsVery cheap (often hardware)Everyday numeric code
sin / pow / logModerate per callFormulas; avoid needless repeats in hot loops
float variants (sinf)Often faster / less memoryLarge 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.

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.

Next: setjmp.h

Learn non-local jumps with setjmp and longjmp.

setjmp.h tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful