C Programming - Beginner to Brilliance / Math & Algorithm Support

math.h Functions

The math.h header in C provides a powerful collection of functions to handle mathematical computations. From basic arithmetic to complex trigonometric, exponential, and logarithmic operations — math.h helps you avoid writing long custom logic for common mathematical tasks.


🔧 How to Use math.h

#include <math.h>

⚠️ Important: When compiling, link with the math library by adding -lm at the end of your gcc command:

gcc program.c -lm

Why? Because math.h functions are part of the libm library (math library), and -lm tells the compiler to link against it.


📚 Categorized Overview of Key math.h Functions

✅ Power and Roots

Function

Description

Example

Output

sqrt(x)

Returns √x (square root)

sqrt(25)

5.0

pow(x, y)

Returns xʸ (x raised to y)

pow(2, 3)

8.0

cbrt(x)

Returns cube root of x

cbrt(27)

3.0

🔍 Examples:

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

int main() {
    printf("Square root: %.2f\\n", sqrt(64));   // 8.00
    printf("Power: %.2f\\n", pow(3, 4));        // 81.00
    return 0;
}

➕ Rounding Functions

Function

Description

Example

Output

floor(x)

Rounds down to nearest integer

floor(4.7)

4.0

ceil(x)

Rounds up to nearest integer

ceil(4.2)

5.0

round(x)

Rounds to nearest integer

round(4.6)

5.0

trunc(x)

Truncates decimal (toward zero)

trunc(5.9)

5.0

🔹 Difference between floor, trunc, and round:

  • floor(-4.3) → 5.0

  • trunc(-4.3) → 4.0

  • round(-4.3) → 4.0


➖ Absolute Value

Function

Description

Example

abs(x)

Absolute value of int

abs(-3) → 3

fabs(x)

Absolute value of float/double

fabs(-5.7) → 5.7


📏 Logarithmic & Exponential Functions

Function

Description

Example

Output

log(x)

Natural log (ln x)

log(2.7183)

≈ 1.0

log10(x)

Log base 10

log10(100)

2.0

exp(x)

Returns eˣ

exp(1)

2.71828

🔍 Use Case:

printf("log(100): %.2f\\n", log10(100));    // 2.00
printf("exp(1): %.2f\\n", exp(1));          // 2.72

📐 Trigonometric Functions

Trigonometric functions use radians, not degrees.

Convert degrees to radians:

radians = degrees * (M_PI / 180.0)

Function

Description

Example

sin(x)

Sine of angle x in radians

sin(M_PI/2) → 1.0

cos(x)

Cosine of x

cos(0) → 1.0

tan(x)

Tangent of x

tan(M_PI/4) → 1.0

Inverse Trigonometric:

Function

Description

asin(x)

Arc sine

acos(x)

Arc cosine

atan(x)

Arc tangent

double angle = M_PI / 3;
printf("cos(60 deg): %.2f\\n", cos(angle));   // 0.5

🧩 Other Helpful Functions

Function

Description

Example

fmod(x, y)

Remainder of x / y (float)

fmod(7.5, 2.0) → 1.5

hypot(x, y)

Returns √(x² + y²)

hypot(3, 4) → 5.0

fmax(x, y)

Returns max

fmax(3, 9) → 9

fmin(x, y)

Returns min

fmin(3, 9) → 3

remainder(x, y)

IEEE remainder

remainder(5.3, 2) → may differ from fmod


📂 Constant Macros in math.h

Macro

Value

M_PI

3.14159265358979323846

M_E

2.71828182845904523536

💡 These are not guaranteed in every compiler. If not found, define them yourself.

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

📌 Real Life Use Cases

  • Game development → Movement, angles, rotations.

  • Scientific computing → Logarithmic or exponential simulations.

  • Finance software → Compound interest (with pow()).

  • Graphics/Animation → Using trigonometry for rotations.

  • Engineering → Calculations involving hypot(), roots, trig.


⚠️ Precision Tips

  • All math.h functions work with double.

    Use sqrtf(), cosf() etc. for float.

  • Use %.2f in printf for formatting:

printf("%.2f", sqrt(10));  // 3.16