Math Functions in C

The C standard library provides mathematical functions for rounding, powers, roots, logarithms, trigonometry and floating-point calculations. Most are declared in <math.h>; abs() for integers is declared in <stdlib.h>.

Compilation note: on many Unix-like systems, programs that use <math.h> functions are linked with the math library, for example gcc program.c -lm.

Common C Math Functions Top ↑

Small Math Example Top ↑

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

int main(void) {
    double value = 5.7;

    printf("floor: %.1f\n", floor(value));
    printf("ceil : %.1f\n", ceil(value));
    printf("sqrt : %.3f\n", sqrt(value));

    return 0;
}

Output

floor: 5.0
ceil : 6.0
sqrt : 2.387

Choose the Right Function Top ↑

  • Use abs() for an integer absolute value.
  • Use floor(), ceil(), round() or trunc() depending on the exact rounding rule required.
  • Use fmod() for floating-point remainders; the % operator is for integer operands.
  • Trigonometric functions use radians, not degrees.
  • Check domain restrictions for functions such as sqrt() and log().




plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer