← GCC/C Index

📐 <math.h> — Mathematical Functions

Provides common math functions for floating-point arithmetic. Link with -lm flag: gcc prog.c -lm. All trig functions work in radians.

Include: #include <math.h> | Compile: gcc -Wall -o prog prog.c

📋 Functions & Macros

NamePrototype / ValueDescription
sqrtdouble sqrt(double x)Square root of x
powdouble pow(double base, double exp)base raised to the power exp
fabsdouble fabs(double x)Absolute value of x (floating-point)
ceildouble ceil(double x)Round up to nearest integer
floordouble floor(double x)Round down to nearest integer
rounddouble round(double x)Round to nearest integer
fmoddouble fmod(double x, double y)Floating-point remainder of x/y
sindouble sin(double x)Sine of x (radians)
cosdouble cos(double x)Cosine of x (radians)
tandouble tan(double x)Tangent of x (radians)
asindouble asin(double x)Arc-sine; result in [-π/2, π/2]
acosdouble acos(double x)Arc-cosine; result in [0, π]
atandouble atan(double x)Arc-tangent of x
atan2double atan2(double y, double x)Arc-tangent of y/x using both signs
expdouble exp(double x)e raised to the power x
logdouble log(double x)Natural logarithm of x
log10double log10(double x)Base-10 logarithm of x
log2double log2(double x)Base-2 logarithm of x
hypotdouble hypot(double x, double y)Hypotenuse sqrt(x²+y²)
cbrtdouble cbrt(double x)Cube root of x

💡 Example Program

Copy, save as demo.c, and compile with the command shown at the bottom.

#include <stdio.h>
#include <math.h>
/* Link with:  gcc -Wall -o math_demo math_demo.c -lm */

#define PI 3.14159265358979323846

int main(void) {
    double x = 2.0;
    printf("sqrt(%.1f)  = %.6f\n", x, sqrt(x));
    printf("pow(2,10)   = %.0f\n",    pow(2, 10));
    printf("sin(PI/6)   = %.6f\n",    sin(PI / 6));
    printf("cos(0)      = %.6f\n",    cos(0.0));
    printf("log(e)      = %.6f\n",    log(exp(1.0)));
    printf("floor(3.9)  = %.1f\n",    floor(3.9));
    printf("ceil(3.1)   = %.1f\n",    ceil(3.1));
    printf("hypot(3,4)  = %.1f\n",    hypot(3.0, 4.0));
    return 0;
}

← <stdio.h>  |  🏠 Index  |  <string.h> →