C Math Functions

C Math Functions
C provides powerful mathematical functions through the math library
Header file: <math.h>
These functions are widely used in:
Scientific calculations
Engineering programs
Graphics
Embedded systems
Competitive programming
How to Use Math Functions in C
Include Header File
Compile with Math Library (Important)
(-lm links the math library)
Common Math Functions (Most Used)
sqrt() – Square Root
pow() – Power
abs() – Absolute Value (int)
fabs() – Absolute Value (float/double)
Trigonometric Functions
(All angles are in radians)
| Function | Description |
|---|---|
sin(x) | Sine |
cos(x) | Cosine |
tan(x) | Tangent |
asin(x) | Inverse sine |
acos(x) | Inverse cosine |
atan(x) | Inverse tangent |
Example
Logarithmic & Exponential Functions
| Function | Meaning |
|---|---|
log(x) | Natural log (ln) |
log10(x) | Base-10 log |
exp(x) | eˣ |
Example
Rounding Functions
| Function | Description |
|---|---|
ceil(x) | Round up |
floor(x) | Round down |
round(x) | Nearest integer |
trunc(x) | Remove decimal |
Example
Min / Max & Remainder
fmod() – Floating Modulus
Integer Math vs Floating Math
- Use floating-point values to get correct results
Math Constants (Using Macros)
(Some compilers also support M_PI, but not standard)
Complete Example Program
Common Mistakes
- Forgetting
#include <math.h> Not using-lmduring compilation- Using
abs()for floating values - Using degrees instead of radians
Interview Questions (Very Important)
Which header file contains math functions?
Difference between
abs()andfabs()?Why
-lmis required?Difference between
ceil()andfloor()?pow()return type?
Real-Life Use Cases
Engineering calculations
Signal processing
3D graphics
Embedded sensors
Financial software
Summary
- Math functions are defined in
<math.h> Use-lmwhile compiling- Trigonometric functions use radians
- Essential for placements & projects
