C Math Functions
What You’ll Find Here
The C standard library provides powerful math functions in <math.h> for roots, powers, logarithms, trigonometry, rounding, and floating-point decomposition. This page is your central hub: browse all 30 functions in our tutorial series, jump to full guides with five examples each, and learn patterns that work in real C programs.
Full Tutorials
30 functions with examples.
Quick Reference
All functions by category.
Searchable
Filter by function name.
Link -lm
GCC/Clang compile tips.
Radians
Trig angle units explained.
Start Here
fabs() → sqrt().
Introduction
In C, you perform floating-point math through library functions in <math.h> such as sqrt(), pow(), sin(), and floor()—not operators like x^2 in some other languages. Most return double; variants like sqrtf() and sinf() work with float.
Integer absolute value abs() lives in <stdlib.h>, while fabs() handles floating-point magnitudes in <math.h>. Know which header each function needs before you compile.
Compile with the math library on GCC/Clang: gcc main.c -std=c11 -o main -lm. Put -lm at the end of the command. Trig functions expect radians—convert degrees with degrees * (M_PI / 180.0).
Math Functions Index
Search by function name or browse by category. Cards marked Tutorial link to full guides with syntax, five examples, output, and FAQs.
Absolute Value
2 functionsRemove the sign from numbers—integer abs() in stdlib.h and floating-point fabs() in math.h.
Roots & Powers
3 functionsSquare roots, cube roots, and raise a number to any exponent.
Exponential & Logarithm
3 functionsNatural exponent, natural log, base-10 log, and e^x growth models.
Circular Trigonometry
7 functionsSine, cosine, tangent, and inverses—all angles in radians unless you convert from degrees.
Hyperbolic Functions
6 functionssinh, cosh, tanh and inverses—built from exponentials; not the same as circular trig despite similar names.
Rounding & Integer Parts
5 functionsRound up, down, to nearest, truncate toward zero, or split whole and fractional parts.
Remainder & Decomposition
4 functionsFloating remainder, split into mantissa and exponent, scale by powers of two, and hypotenuse length.
🚀 Usage Tips
- Link with -lm — on GCC/Clang:
gcc file.c -std=c11 -o out -lm. - Use radians for trig — convert degrees:
rad = deg * M_PI / 180.0. - Pick the right abs —
abs()forint,fabs()fordouble. - Compare rounding functions —
floor,ceil,round, andtruncdiffer on negatives and halves. - Prefer atan2 over atan —
atan2(y, x)handles all quadrants for slope angles. - Read the tutorial — each linked page includes syntax, five examples, a quick reference table, and FAQs.
📝 How to Call a Math Function
Every math.h function follows the same general pattern:
#include <math.h>
double result = function_name(arguments); Common Patterns
| Goal | Example |
|---|---|
| Square root | sqrt(25.0) → 5.0 |
| Power | pow(2.0, 10.0) → 1024.0 |
| Sine of 90° | sin(M_PI / 2.0) → 1.0 |
| Float absolute value | fabs(-3.14) → 3.14 |
| Truncate decimals | trunc(3.75) → 3.0 |
math.h vs stdlib.h
Not every numeric helper lives in <math.h>. Integer abs(), labs(), and llabs() are declared in <stdlib.h>. Floating-point math—roots, trig, rounding, fabs()—comes from <math.h>.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int n = -42;
double x = -3.14;
printf("abs(%d) = %d\n", n, abs(n));
printf("fabs(%g) = %g\n", x, fabs(x));
printf("sqrt(16) = %g\n", sqrt(16.0));
return 0;
}
/* Compile: gcc demo.c -std=c11 -o demo -lm */ Conclusion
C math functions cover roots, powers, logarithms, trigonometry, hyperbolic curves, rounding, and remainders. Use this index to find the right function quickly, then open the full tutorial for syntax, examples, and best practices.
New to C math? Read the C Math overview first, then start with fabs() and sqrt() before diving into sin() and pow().
❓ Frequently Asked Questions
Tip: Most C math functions live in <math.h> and return double—for example sqrt(x) or sin(angle_rad). On GCC and Clang you often must link with -lm: gcc program.c -o program -lm. Circular trig functions take radians, not degrees.
Start Your First Math Function Tutorial
Open the fabs() guide—syntax, five examples, and a quick reference cheat sheet.
30 people found this page helpful
