Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 4 — Standard Library: math.h

Lesson 21 of 32 in the free C Language notes on Siksha Sarovar, written by Rohit Jangra.

Standard Library: math.h

The math.h header provides mathematical functions. Compile with -lm flag on Linux/GCC.

gcc program.c -o program -lm

---

Basic Functions

FunctionDescriptionExample
sqrt(x)Square rootsqrt(16) = 4.0
pow(x, y)x raised to ypow(2, 10) = 1024.0
abs(x)Absolute value (int)abs(-5) = 5
fabs(x)Absolute value (float)fabs(-3.14) = 3.14
ceil(x)Round upceil(3.2) = 4.0
floor(x)Round downfloor(3.9) = 3.0
round(x)Round to nearestround(3.5) = 4.0

---

Trigonometric Functions

All functions take angles in radians.

FunctionDescription
sin(x)Sine
cos(x)Cosine
tan(x)Tangent
asin(x)Arcsine
acos(x)Arccosine
atan(x)Arctangent
#define PI 3.14159265
double angle = 30.0 * PI / 180.0;  /* convert degrees to radians */
printf("sin(30) = %.4f\n", sin(angle));  /* 0.5000 */

---

Logarithmic and Exponential

FunctionDescriptionExample
log(x)Natural log (base e)log(2.718) ≈ 1.0
log10(x)Log base 10log10(100) = 2.0
log2(x)Log base 2 (C99)log2(8) = 3.0
exp(x)e raised to xexp(1) ≈ 2.718

---

Constants in math.h

ConstantValue
M_PI3.14159265358979
M_E2.71828182845905
M_SQRT21.41421356237310
Note: M_PI etc. are not part of ANSI C standard; use #define _USE_MATH_DEFINES on Windows or define your own.

---

Example Program

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

int main() {
    double a = 3.0, b = 4.0;
    double hypotenuse = sqrt(a*a + b*b);  /* Pythagorean theorem */
    
    printf("Hypotenuse = %.2f\n", hypotenuse);  /* 5.00 */
    printf("log10(1000) = %.1f\n", log10(1000)); /* 3.0 */
    printf("2^8 = %.0f\n", pow(2, 8));           /* 256 */
    
    return 0;
}