Lesson 28: Elementary Functions (math.h)

The C math.h library — how to use sqrt, sin, cos, pow, and other math functions.

📖 What you'll learn on this page
✅ Must-know essentials
  • Include <math.h> and link with -lm
  • Use sqrt, pow, sin, cos, log, and exp
  • Trig functions take radians, not degrees
⭐ Read if you have time
  • M_PI and M_E constants
  • fabs, floor, ceil, and round
  • Handling NaN and Inf

What are Elementary Functions?

C ships with a familiar set of math functions: square roots, trigonometric functions, exponentials, logarithms, and more. To use them, include <math.h>.
How to use them: (1) add #include <math.h> at the top, (2) call functionName(arg), and (3) the return value is almost always a double.
#include<stdio.h>
#include<math.h>  // ← required!

int main(void){
  double x = 2.0;
  printf("sqrt(%.1f) = %f\n", x, sqrt(x));
  return 0;
}
Compile note: On Linux and Mac, gcc may require the -lm option to link the math library: gcc prog.c -lm. Visual Studio doesn't need it.

Main Math Functions

All of these become available once you #include <math.h>. Arguments and return values are essentially always double.
Function Purpose Example Result
sqrt(x)Square root √xsqrt(9.0)3.0
pow(x, y)Power xypow(2.0, 10.0)1024.0
fabs(x)Absolute value |x|fabs(-3.7)3.7
sin(x)Sine (radians)sin(3.14159/2)1.0
cos(x)Cosine (radians)cos(0.0)1.0
tan(x)Tangent (radians)tan(3.14159/4)≈ 1.0
exp(x)Exponential exexp(1.0)≈ 2.71828
log(x)Natural log ln(x)log(2.71828)≈ 1.0
log10(x)Common log log10(x)log10(1000.0)3.0
ceil(x)Round upceil(2.3)3.0
floor(x)Round downfloor(2.7)2.0

sqrt — Square Root

Our first function. sqrt(x) returns the square root of x (√x).
#include<stdio.h>
#include<math.h>

int main(void){
  double a = 2.0;
  printf("sqrt(%.1f) = %f\n", a, sqrt(a));
  // -> sqrt(2.0) = 1.414214

  // Pythagorean theorem: c = sqrt(a^2 + b^2)
  double b = 3.0, c = 4.0;
  double hyp = sqrt(b*b + c*c);
  printf("hypotenuse = %f\n", hyp);
  // -> hypotenuse = 5.000000
  return 0;
}
Note: Passing a negative number to sqrt returns NaN (Not a Number). It's safer to check the sign beforehand.

Trigonometric Functions (sin / cos / tan)

C's trig functions take radians, not degrees. Convert degrees to radians with rad = deg * M_PI / 180.0.
sin(θ)
sin(0) = 0
sin(π/2) = 1
sin(π) = 0
cos(θ)
cos(0) = 1
cos(π/2) = 0
cos(π) = -1
tan(θ)
tan(0) = 0
tan(π/4) ≈ 1
tan(π/2) = ∞
#include<math.h>

// degrees -> radians
double deg = 45.0;
double rad = deg * M_PI / 180.0;

printf("sin(45 deg) = %f\n", sin(rad)); // -> 0.707107
printf("cos(45 deg) = %f\n", cos(rad)); // -> 0.707107
printf("tan(45 deg) = %f\n", tan(rad)); // -> 1.000000
Some implementations provide M_PI and M_E in math.h, but standard C does not require them. For portable code, declare const double pi = acos(-1.0); and replace M_PI with pi. Use exp(1.0) for e.

Function Calculator — Try It

Try the math functions right in your browser. Pick a function and enter a value to see the result.
The result will appear here.

Try it yourself: math functions

Use trigonometric functions and square roots to calculate a right triangle's hypotenuse and angles.
mathfunc.c
Output
Press Run…
💡 More things to try
← Previous lesson
Lesson 27: Functions (Advanced)
Next lesson →
Lesson 29: Random Number Generation

Related lessons

Loops, Arrays, Strings
Lesson 29: Random Number Generation
How to use rand() and srand(). Ranges, seeds, and applications illustrated.
Basics
Lesson 3: Variables
What are C variables? int, double, char explained with diagrams.

Review Quiz

Check your understanding.

Q1. Which header do you need to use math functions?

stdlib.h
math.h
string.h

Math functions like sqrt, pow, sin, and cos are declared in math.h. You may also need the -lm option when compiling on Linux or Mac.

Q2. What is the result of pow(2, 3)?

6
8
9

pow(2, 3) returns 2 raised to the 3rd power, which is 8. pow returns a double.

Q3. What is the result of sqrt(16)?

4.0
8.0
256.0

sqrt returns the square root. √16 = 4.0.

Share this article
Share on X Share on Facebook