Turning Learners Into Developers
Codekilla
CODEKILLA
C Language 8 min

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.

Rahul Chaudhary Thu Apr 30 2026
What is the C Math Library?

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.

Why It Matters
  • 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
Power and Root Functions

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.

FunctionPurposeReturns
pow(x, y)x raised to power yx^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
Trigonometric Functions

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.

FunctionPurposeInput Range
sin(x)Sine of xAny radian value
cos(x)Cosine of xAny radian value
tan(x)Tangent of xAvoid 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/xAny 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
Exponential and Logarithmic Functions

Exponentials grow rapidly, while logarithms are their inverse operations. These are fundamental in algorithms involving growth rates, complexity analysis, and probability calculations.

FunctionPurposeNotes
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
Rounding and Absolute Value Functions

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.

FunctionBehaviorExample: -2.7Example: 2.3
ceil(x)Rounds up to nearest integer-2.03.0
floor(x)Rounds down to nearest integer-3.02.0
round(x)Rounds to nearest integer-3.02.0
trunc(x)Removes decimal part-2.02.0
fabs(x)Absolute value2.72.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

Hyperbolic functions appear in calculus, physics (especially relativity), and engineering. They relate to hyperbolas the same way trigonometric functions relate to circles.

FunctionPurpose
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)
Remainder and Modulo Functions

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.

FunctionPurposeDifference
fmod(x, y)Floating-point remainderSign matches dividend (x)
remainder(x, y)IEEE remainderRounds 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°
Quick Cheat Sheet
NeedReach for
Square/cube rootsqrt(), cbrt()
Any powerpow(base, exponent)
Trig with degreesConvert: angle * PI / 180.0, then sin(), cos(), tan()
Trig inverseasin(), acos(), atan(), atan2(y, x) for quadrant-aware results
Round to integerceil() (up), floor() (down), round() (nearest)
Remove decimalstrunc()
Positive value onlyfabs()
e^x or ln(x)exp(), log()
log base 10 or 2log10(), log2()
Float modulofmod(dividend, divisor)
Check for NaN/infinityisnan(), isinf()
Common Mistakes
  • Forgetting to link the math library — Compile with gcc program.c -lm on Linux/Unix, or you'll get "undefined reference" errors. On Windows with some compilers, linking happens automatically.

  • Passing degrees to trig functionssin(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 functionssqrt(1/2) evaluates to sqrt(0) because 1/2 is integer division. Use sqrt(1.0/2.0) to force floating-point division.

  • Ignoring domain restrictions — Calling sqrt(-1) or log(-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.3 can be false. Use fabs(a - b) < 0.00001 for comparisons instead.

  • Not handling special values — Functions can return INFINITY, -INFINITY, or NaN. Use isinf() and isnan() 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.

// was this useful?
Did this article answer your question?
// C Language · published by Codekilla
// related articles

Keep Reading