C Math Functions
Introduction to Math Functions
C provides a rich set of mathematical functions through the math.h header file. These functions allow you to perform complex mathematical operations including trigonometric functions, exponential and logarithmic functions, power functions, rounding, and absolute value calculations.
To use these functions, you need to include the math.h header and (on many Unix-like systems with GCC/Clang) link with the math library using the -lm flag during compilation.
Including Math Library
Example
#include <stdio.h>
#include <math.h> // Required for math functions
int main(void) {
double result = sqrt(25.0);
printf("Square root of 25 is: %.2f\n", result);
return 0;
}
Output
Square root of 25 is: 5.00
ℹ️ Note: Compile with: gcc program.c -o program -lm (GCC/Clang on Linux). On macOS and MSVC/Windows, -lm is usually not required.
Common Math Functions
Function | Description | Example |
---|---|---|
sqrt(x) | Square root of x | sqrt(16.0) = 4.0 |
pow(x, y) | x raised to power y | pow(2.0, 3.0) = 8.0 |
fabs(x) | Absolute value of x | fabs(-5.5) = 5.5 |
ceil(x) | Smallest integer ≥ x (as double) | ceil(3.2) = 4.0 |
floor(x) | Largest integer ≤ x (as double) | floor(3.8) = 3.0 |
sin(x), cos(x), tan(x) | Trigonometric functions (x in radians) | sin(pi/2) = 1.0 |
Trigonometric Functions Example
Example
#include <stdio.h>
#include <math.h>
int main(void) {
double angle = 45.0; // degrees
// Portable way to get pi without relying on non-standard M_PI
double pi = acos(-1.0);
double radians = angle * pi / 180.0; // convert to radians
printf("Angle: %.0f degrees\n", angle);
printf("Sine: %.3f\n", sin(radians));
printf("Cosine: %.3f\n", cos(radians));
printf("Tangent: %.3f\n", tan(radians));
return 0;
}
Output
Angle: 45 degrees Sine: 0.707 Cosine: 0.707 Tangent: 1.000
Exponential and Logarithmic Functions
Example
#include <stdio.h>
#include <math.h>
int main(void) {
double x = 2.5;
printf("e^%.1f = %.3f\n", x, exp(x));
printf("Natural log of %.1f = %.3f\n", x, log(x));
printf("Log base 10 of %.1f = %.3f\n", x, log10(x));
return 0;
}
Output
e^2.5 = 12.182 Natural log of 2.5 = 0.916 Log base 10 of 2.5 = 0.398
Rounding and Absolute Value
Example
#include <stdio.h>
#include <math.h>
int main(void) {
double num = -7.89;
printf("Original: %.2f\n", num);
printf("Absolute value: %.2f\n", fabs(num));
printf("Rounded to nearest: %.0f\n", round(num));
printf("Ceiling: %.0f\n", ceil(num));
printf("Floor: %.0f\n", floor(num));
return 0;
}
Output
Original: -7.89 Absolute value: 7.89 Rounded to nearest: -8 Ceiling: -7 Floor: -8
Mathematical Constants
The C standard does not require macros like M_PI to be defined in
Example
#include <stdio.h>
#include <math.h>
int main(void) {
// Portable computations of common constants
double pi = acos(-1.0);
double e = exp(1.0);
double sqrt2 = sqrt(2.0);
double ln2 = log(2.0);
printf("Pi: %.10f\n", pi);
printf("Euler's number (e): %.10f\n", e);
printf("Square root of 2: %.10f\n", sqrt2);
printf("Natural log of 2: %.10f\n", ln2);
return 0;
}
Output
Pi: 3.1415926536 Euler's number (e): 2.7182818285 Square root of 2: 1.4142135624 Natural log of 2: 0.6931471806
ℹ️ Note: If you prefer macros like M_PI on MSVC, define #define _USE_MATH_DEFINES before including <math.h>. On some Unix-like systems, extra feature macros may be needed. Portable code should not rely on these macros.