Thuta Learning
IntermediateProgrammingbeginner

Math Functions

Relax. We'll talk through this in plain words — no textbook voice.

The <math.h> header is packed with handy functions for math calculations. Square roots, rounding, powers, trigonometry — you get all of it without writing the formulas yourself.

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

int main() {
  printf("Square root: %.1f
", sqrt(16));
  printf("Rounded: %.0f
", round(2.6));
  printf("Power: %.0f", pow(4, 2));
  return 0;
}

sqrt(16) calculates the square root of 16. round(2.6) rounds to the nearest whole number, and pow(4, 2) calculates 4 to the power of 2.

You should see
Square root: 4.0 Rounded: 3 Power: 16

Info

On Linux with GCC, you may need to link the math library with gcc file.c -lm. Most online compilers handle this automatically for you.

Easy traps

  • Using a math function without including <math.h> can trigger warnings or errors.
Math Functions | Thuta Learning