All C Math Functions with Examples and Outputs
Read on to explore all c math functions with examples and outputs — a beginner-friendly walkthrough by Codekilla.
The C math library (math.h) is a standard header file that provides a collection of mathematical functions for performing common calculations. When you include #include <math.h> at the top of your program, you unlock access to trigonometric operations, logarithms, exponentials, rounding functions, and much more. Think of it as your mathematical Swiss Army knife—instead of manually coding complex formulas, you call pre-built, optimized functions that handle everything from square roots to hyperbolic tangents.
These functions work primarily with double precision floating-point numbers, though some variants exist for float and long double. When compiling programs that use math functions, you'll often need to link the math library explicitly with -lm flag (especially on Linux/Unix systems). This library has been part of the C standard since the beginning, making it universally available across platforms.
- Saves development time — No need to implement complex algorithms for trigonometry, exponentials, or logarithms from scratch
- Scientific computing foundation — Essential for physics simulations, financial calculations, graphics rendering, and statistical analysis
- Standardized accuracy — Functions are tested and optimized for precision across different hardware platforms
- Performance critical applications — Game engines, data science tools, and embedded systems rely heavily on these optimized mathematical operations
- Educational value — Understanding these functions deepens your grasp of how computers handle numerical computation
These are your go-to tools for exponentiation and finding roots. The pow() function raises numbers to any power, while sqrt() and cbrt() handle square and cube roots specifically.
| Function | Purpose | Returns |
|---|---|---|
pow(x, y) | x raised to power y | x^y |
sqrt(x) | Square root of x | √x |
cbrt(x) | Cube root of x | ∛x |
c#include <stdio.h> #include <math.h> int main() { double base = 2.0, exponent = 3.0; printf("2^3 = %.2f\n", pow(base, exponent)); printf("√16 = %.2f\n", sqrt(16.0)); printf("∛27 = %.2f\n", cbrt(27.0)); printf("2^0.5 (same as √2) = %.4f\n", pow(2.0, 0.5)); return 0; }
Output:
2^3 = 8.00
√16 = 4.00
∛27 = 3.00
2^0.5 (same as √2) = 1.4142
Trigonometry functions work with angles measured in radians, not degrees. Remember that π radians equals 180 degrees. These functions are critical for game development, computer graphics, and any physics-based simulation.
| Function | Purpose | Input Range |
|---|---|---|
sin(x) | Sine of x | Any radian value |
cos(x) | Cosine of x | Any radian value |
tan(x) | Tangent of x | Avoid multiples of π/2 |
asin(x) | Arc sine (inverse) | [-1, 1] |
acos(x) | Arc cosine (inverse) | [-1, 1] |
atan(x) | Arc tangent (inverse) | Any value |
atan2(y, x) | Arc tangent of y/x | Any values |
c#include <stdio.h> #include <math.h> #define PI 3.14159265358979323846 int main() { double angle_deg = 45.0; double angle_rad = angle_deg * PI / 180.0; printf("Angle: %.0f degrees = %.4f radians\n", angle_deg, angle_rad); printf("sin(45°) = %.4f\n", sin(angle_rad)); printf("cos(45°) = %.4f\n", cos(angle_rad)); printf("tan(45°) = %.4f\n", tan(angle_rad)); // Inverse functions return radians double value = 0.5; printf("\narcsin(0.5) = %.4f radians = %.0f degrees\n", asin(value), asin(value) * 180.0 / PI); return 0; }
Output:
Angle: 45 degrees = 0.7854 radians
sin(45°) = 0.7071
cos(45°) = 0.7071
tan(45°) = 1.0000
arcsin(0.5) = 0.5236 radians = 30 degrees
Exponentials grow rapidly, while logarithms are their inverse operations. These are fundamental in algorithms involving growth rates, complexity analysis, and probability calculations.
| Function | Purpose | Notes |
|---|---|---|
exp(x) | e^x (Euler's number) | Natural exponential |
log(x) | Natural logarithm (base e) | Input must be > 0 |
log10(x) | Common logarithm (base 10) | Input must be > 0 |
log2(x) | Binary logarithm (base 2) | Input must be > 0 |
c#include <stdio.h> #include <math.h> int main() { double x = 2.0; printf("e^2 = %.4f\n", exp(x)); printf("ln(e^2) = %.4f\n", log(exp(x))); printf("log₁₀(100) = %.1f\n", log10(100.0)); printf("log₂(8) = %.1f\n", log2(8.0)); // Logarithm identity: logₐ(b) = ln(b) / ln(a) double log_base_5_of_25 = log(25.0) / log(5.0); printf("log₅(25) = %.1f\n", log_base_5_of_25); return 0; }
Output:
e^2 = 7.3891
ln(e^2) = 2.0000
log₁₀(100) = 2.0
log₂(8) = 3.0
log₅(25) = 2.0
When you need to control decimal precision or remove signs, these functions are your allies. Each rounding function behaves differently—understanding the distinction prevents subtle bugs in calculations.
| Function | Behavior | Example: -2.7 | Example: 2.3 |
|---|---|---|---|
ceil(x) | Rounds up to nearest integer | -2.0 | 3.0 |
floor(x) | Rounds down to nearest integer | -3.0 | 2.0 |
round(x) | Rounds to nearest integer | -3.0 | 2.0 |
trunc(x) | Removes decimal part | -2.0 | 2.0 |
fabs(x) | Absolute value | 2.7 | 2.3 |
c#include <stdio.h> #include <math.h> int main() { double positive = 3.7; double negative = -3.7; printf("Original values: %.1f and %.1f\n\n", positive, negative); printf("ceil(3.7) = %.1f, ceil(-3.7) = %.1f\n", ceil(positive), ceil(negative)); printf("floor(3.7) = %.1f, floor(-3.7) = %.1f\n", floor(positive), floor(negative)); printf("round(3.7) = %.1f, round(-3.7) = %.1f\n", round(positive), round(negative)); printf("trunc(3.7) = %.1f, trunc(-3.7) = %.1f\n", trunc(positive), trunc(negative)); printf("fabs(-3.7) = %.1f\n", fabs(negative)); return 0; }
Output:
Original values: 3.7 and -3.7
ceil(3.7) = 4.0, ceil(-3.7) = -3.0
floor(3.7) = 3.0, floor(-3.7) = -4.0
round(3.7) = 4.0, round(-3.7) = -4.0
trunc(3.7) = 3.0, trunc(-3.7) = -3.0
fabs(-3.7) = 3.7
Hyperbolic functions appear in calculus, physics (especially relativity), and engineering. They relate to hyperbolas the same way trigonometric functions relate to circles.
| Function | Purpose |
|---|---|
sinh(x) | Hyperbolic sine |
cosh(x) | Hyperbolic cosine |
tanh(x) | Hyperbolic tangent |
asinh(x) | Inverse hyperbolic sine |
acosh(x) | Inverse hyperbolic cosine |
atanh(x) | Inverse hyperbolic tangent |
c#include <stdio.h> #include <math.h> int main() { double x = 1.0; printf("sinh(1) = %.4f\n", sinh(x)); printf("cosh(1) = %.4f\n", cosh(x)); printf("tanh(1) = %.4f\n", tanh(x)); // Hyperbolic identity: cosh²(x) - sinh²(x) = 1 double identity = pow(cosh(x), 2) - pow(sinh(x), 2); printf("\ncosh²(1) - sinh²(1) = %.4f (should be 1)\n", identity); return 0; }
Output:
sinh(1) = 1.1752
cosh(1) = 1.5431
tanh(1) = 0.7616
cosh²(1) - sinh²(1) = 1.0000 (should be 1)
Unlike the % operator (which only works with integers), these functions handle floating-point modulo operations. They're useful when you need to wrap angles, implement cyclic buffers, or perform periodic calculations.
| Function | Purpose | Difference |
|---|---|---|
fmod(x, y) | Floating-point remainder | Sign matches dividend (x) |
remainder(x, y) | IEEE remainder | Rounds to nearest, can be negative |
c#include <stdio.h> #include <math.h> int main() { double x = 7.5, y = 2.3; printf("fmod(7.5, 2.3) = %.4f\n", fmod(x, y)); printf("fmod(-7.5, 2.3) = %.4f\n", fmod(-x, y)); printf("remainder(7.5, 2.3) = %.4f\n", remainder(x, y)); // Wrapping an angle to [0, 360) double angle = 385.0; double wrapped = fmod(angle, 360.0); printf("\nAngle 385° wrapped to [0,360): %.0f°\n", wrapped); return 0; }
Output:
fmod(7.5, 2.3) = 0.6000
fmod(-7.5, 2.3) = -0.6000
remainder(7.5, 2.3) = 0.6000
Angle 385° wrapped to [0,360): 25°
| Need | Reach for |
|---|---|
| Square/cube root | sqrt(), cbrt() |
| Any power | pow(base, exponent) |
| Trig with degrees | Convert: angle * PI / 180.0, then sin(), cos(), tan() |
| Trig inverse | asin(), acos(), atan(), atan2(y, x) for quadrant-aware results |
| Round to integer | ceil() (up), floor() (down), round() (nearest) |
| Remove decimals | trunc() |
| Positive value only | fabs() |
| e^x or ln(x) | exp(), log() |
| log base 10 or 2 | log10(), log2() |
| Float modulo | fmod(dividend, divisor) |
| Check for NaN/infinity | isnan(), isinf() |
-
Forgetting to link the math library — Compile with
gcc program.c -lmon Linux/Unix, or you'll get "undefined reference" errors. On Windows with some compilers, linking happens automatically. -
Passing degrees to trig functions —
sin(45)doesn't give you sin of 45 degrees—it's sin of 45 radians. Always convert:sin(45 * PI / 180.0). -
Using integer division before passing to math functions —
sqrt(1/2)evaluates tosqrt(0)because1/2is integer division. Usesqrt(1.0/2.0)to force floating-point division. -
Ignoring domain restrictions — Calling
sqrt(-1)orlog(-5)returns NaN (Not a Number). Check inputs when working with user data or calculations that might produce negatives. -
Comparing floating-point results with
==— Due to precision limits,0.1 + 0.2 == 0.3can be false. Usefabs(a - b) < 0.00001for comparisons instead. -
Not handling special values — Functions can return
INFINITY,-INFINITY, orNaN. Useisinf()andisnan()to check results when precision matters.
💡 Think Like a Programmer: The math library is your shortcut to numerical correctness—use it instead of reinventing the wheel, but always validate inputs and understand the precision limits of floating-point arithmetic.
Keep Reading
C Preprocessor Directives Explained with Examples
Read on to explore c preprocessor directives explained with examples — a beginner-friendly walkthrough by Codekilla.
All C Language Keywords Explained with Examples
Read on to explore all c language keywords explained with examples — a beginner-friendly walkthrough by Codekilla.
C stdio.h Functions List with Examples
Read on to explore c stdio.h functions list with examples — a beginner-friendly walkthrough by Codekilla.
