C math.h Header

Mathematical functions and constants

🧮 What is math.h?

The math.h header provides mathematical functions for calculations including trigonometry, logarithms, exponentials, and power operations. Essential for scientific and engineering computations in C programs.


#include <math.h>

int main() {
    double result = sqrt(16.0);
    printf("Square root of 16: %.2f\n", result);
    printf("Power 2^3: %.2f\n", pow(2, 3));
    return 0;
}
                                    

Output:

Square root of 16: 4.00
Power 2^3: 8.00

Key math.h Functions

sqrt()

Calculate square root

double result = sqrt(25.0);
^

pow()

Calculate power (x^y)

double result = pow(2, 3);
📐

sin(), cos()

Trigonometric functions

double s = sin(M_PI/2);
📊

log(), exp()

Logarithmic and exponential

double l = log(10.0);

🔹 Basic Mathematical Operations

Common mathematical calculations using math.h functions enable scientific computing, engineering simulations, and complex algorithm implementations. The library provides functions for power, square root, logarithms, and absolute value calculations. Use pow() for exponentiation, sqrt() for square roots, and fabs() for floating-point absolute values. Link with -lm flag when compiling. Mathematical operations are fundamental for physics simulations, financial modeling, and graphics programming applications.

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

int main() {
    double x = 16.0, y = 3.0;
    
    // Basic operations
    printf("Square root of %.1f: %.2f\n", x, sqrt(x));
    printf("%.1f raised to power %.1f: %.2f\n", x, y, pow(x, y));
    printf("Absolute value of -5.7: %.2f\n", fabs(-5.7));
    
    // Rounding functions
    printf("Ceiling of 4.3: %.0f\n", ceil(4.3));
    printf("Floor of 4.7: %.0f\n", floor(4.7));
    printf("Round 4.6: %.0f\n", round(4.6));
    
    return 0;
}

Output:

Square root of 16.0: 4.00
16.0 raised to power 3.0: 4096.00
Absolute value of -5.7: 5.70
Ceiling of 4.3: 5
Floor of 4.7: 4
Round 4.6: 5

🔹 Trigonometric Functions

Working with trigonometric functions including sine, cosine, tangent enables angle calculations, rotation operations, and wave simulations in applications. Functions sin(), cos(), and tan() work with radians rather than degrees. Use conversion formula radians = degrees * M_PI / 180 when needed. Inverse functions asin(), acos(), and atan() recover angles from ratios. Trigonometric calculations are essential for graphics, game development, physics simulations, and signal processing applications.

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

int main() {
    double angle_deg = 45.0;
    double angle_rad = angle_deg * M_PI / 180.0; // Convert to radians
    
    printf("Angle: %.1f degrees (%.4f radians)\n", angle_deg, angle_rad);
    printf("sin(%.1f°) = %.4f\n", angle_deg, sin(angle_rad));
    printf("cos(%.1f°) = %.4f\n", angle_deg, cos(angle_rad));
    printf("tan(%.1f°) = %.4f\n", angle_deg, tan(angle_rad));
    
    // Inverse functions
    double value = 0.5;
    printf("arcsin(%.1f) = %.2f degrees\n", value, asin(value) * 180.0 / M_PI);
    
    return 0;
}

Output:

Angle: 45.0 degrees (0.7854 radians)
sin(45.0°) = 0.7071
cos(45.0°) = 0.7071
tan(45.0°) = 1.0000
arcsin(0.5) = 30.00 degrees

🧠 Test Your Knowledge

Which function calculates the square root?