🇯🇵 日本語 | 🇺🇸 English
Advertisement

Lesson 16: Elementary Functions (math.h)

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

What are Elementary Functions?

C comes with a familiar set of math functions built in: square roots, trigonometric functions, exponentials, logarithms, and more. To use them you need #include<math.h>.
Usage: (1) put #include<math.h> at the top, (2) call functionName(arg), (3) the return value is basically 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/Mac, gcc may require the -lm option (to link the math library): gcc prog.c -lm. Visual Studio does not need it.

Main Math Functions

All of these are available via #include<math.h>. Arguments and return values are basically 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

The first function we introduce. 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). Checking the sign in advance is safer.

Trigonometric Functions (sin / cos / tan)

C's trig functions take radians, not degrees. To convert degrees to radians, use 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
M_PI is a pi constant (3.14159...) defined in math.h. You could #define PI 3.14159 yourself, but M_PI is more precise.

Function Calculator — Try It

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

Write Your Own — Math Functions

A program that uses trig functions and square roots. It computes the hypotenuse and angles of a right triangle.
#include<stdio.h>
#include<math.h>

int main(void){
  double a = 3.0;
  double b = 4.0;
  double c = sqrt(a*a + b*b);
  printf("a=%.1f, b=%.1f\n", a, b);
  printf("hypotenuse c = %f\n", c);

  double deg = 30.0;
  double rad = deg * 3.14159 / 180.0;
  printf("sin(%.0f) = %f\n", deg, sin(rad));
  printf("cos(%.0f) = %f\n", deg, cos(rad));

  printf("pow(2,10) = %.0f\n", pow(2.0, 10.0));
  printf("log(100) = %f\n", log10(100.0));
  return 0;
}
Advertisement

Related lessons

Loops, Arrays, Strings
Lesson 17: 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.

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.

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

6
8
9

pow(2, 3) returns 2 to the power 3 = 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