C math.h Library

C math.h Library

The math.h library in C provides a set of mathematical functions for calculations like exponentiation, logarithms, trigonometry, rounding, and more. It’s widely used in scientific, engineering, and graphics applications.

Note: Always compile with -lm flag in GCC to link the math library.
Example: gcc program.c -o program -lm


📌 Common Math Functions

1️⃣ Basic Arithmetic Functions

Function Description
abs(int x) Returns absolute value of an integer
fabs(double x) Returns absolute value of a floating-point number
fmod(double x, double y) Returns remainder of x/y

Example:

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

int main() {
int a = -10;
double b = -5.5;

printf("abs(a) = %d\n", abs(a));
printf("fabs(b) = %.2f\n", fabs(b));
printf("fmod(10.5, 3) = %.2f\n", fmod(10.5, 3));

return 0;
}


2️⃣ Power and Root Functions

Function Description
pow(double x, double y) Returns x raised to the power y
sqrt(double x) Returns square root of x
cbrt(double x) Returns cube root of x

Example:

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

int main() {
double x = 9, y = 2;

printf("pow(x,y) = %.2f\n", pow(x,y));
printf("sqrt(x) = %.2f\n", sqrt(x));
printf("cbrt(27) = %.2f\n", cbrt(27));

return 0;
}


3️⃣ Exponential and Logarithmic Functions

Function Description
exp(double x) Returns e^x
log(double x) Natural logarithm (ln)
log10(double x) Base-10 logarithm

Example:

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

int main() {
double x = 2;

printf("exp(x) = %.2f\n", exp(x));
printf("log(x) = %.2f\n", log(x));
printf("log10(x) = %.2f\n", log10(x));

return 0;
}


4️⃣ Trigonometric Functions

Function Description
sin(double x) Sine of x (radians)
cos(double x) Cosine of x
tan(double x) Tangent of x
asin(double x) Arcsine (inverse sine)
acos(double x) Arccosine
atan(double x) Arctangent
atan2(double y, double x) Arctangent of y/x with correct quadrant

Example:

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

int main() {
double angle = M_PI/4; // 45 degrees in radians

printf("sin(45°) = %.2f\n", sin(angle));
printf("cos(45°) = %.2f\n", cos(angle));
printf("tan(45°) = %.2f\n", tan(angle));

return 0;
}

Note: M_PI is defined in math.h for the value of π.


5️⃣ Rounding and Ceiling/Floor Functions

Function Description
ceil(double x) Returns smallest integer >= x
floor(double x) Returns largest integer <= x
round(double x) Rounds to nearest integer
trunc(double x) Truncates fractional part
fabs(double x) Absolute value (floating-point)

Example:

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

int main() {
double x = 5.7, y = -3.2;

printf("ceil(5.7) = %.2f\n", ceil(x));
printf("floor(5.7) = %.2f\n", floor(x));
printf("round(5.7) = %.2f\n", round(x));
printf("trunc(-3.2) = %.2f\n", trunc(y));

return 0;
}


6️⃣ Other Useful Functions

Function Description
hypot(double x, double y) Returns √(x² + y²)
pow10(double x) Returns 10^x (sometimes powl for long double)
fmax(double x, double y) Returns maximum of two numbers
fmin(double x, double y) Returns minimum of two numbers

Example:

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

int main() {
printf("hypot(3,4) = %.2f\n", hypot(3,4)); // 5
printf("fmax(5,10) = %.2f\n", fmax(5,10));
printf("fmin(5,10) = %.2f\n", fmin(5,10));
return 0;
}


📌 Summary Table

Category Functions
Absolute Value abs, fabs
Power & Roots pow, sqrt, cbrt
Log & Exp log, log10, exp
Trigonometry sin, cos, tan, asin, acos, atan, atan2
Rounding ceil, floor, round, trunc
Misc fmod, hypot, fmax, fmin

✅ Notes

  1. Trigonometric functions use radians, not degrees.

  2. Always compile with -lm to link math library.

  3. For high-precision, use long double versions: powl, sqrtl, etc.

  4. Use fmod instead of % for floating-point remainder.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *