← Back to C Course | Chapter 5: Functions | Lesson 8 of 8

C Math Functions

The math.h library is C's scientific calculator, with things like square roots, powers and rounding. It does the math the simple operators cannot.
Syntax
c
#include <math.h>

sqrt(number);
pow(base, exponent);
fabs(number);

Including math.h

Every function in math.h operates on floating-point numbers and requires you to link the math library at compile time (with -lm on most Unix compilers).

Forgetting the header leaves you with implicit-declaration warnings and undefined behavior on some platforms.

Example: Including math.h

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%.2f", sqrt(16.0));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Square Root (sqrt)

sqrt() only accepts non-negative arguments -- passing a negative number produces NaN rather than a compile error, so validating input before calling it is good practice.

It's commonly used for distance formulas and statistical calculations like standard deviation.

Example: Square Root (sqrt)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%.2f", sqrt(25.0));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Power Function (pow)

Unlike the exponent operator in languages such as Python, C has no ** syntax, so pow(base, exponent) is the only built-in way to raise a number to a power. Both arguments and the return value are doubles, even when you're working with whole numbers.

Example: Power Function (pow)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%.1f", pow(2.0, 3.0));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Rounding Functions (ceil and floor)

ceil(4.1) returns 5.0 while floor(4.9) returns 4.0 -- both always move away from or toward zero along the number line rather than rounding to the nearest integer.

They're essential when you need predictable rounding behavior, such as computing how many pages a printout needs.

Example: Rounding Functions (ceil and floor)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%.1f %.1f", ceil(4.1), floor(4.9));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Absolute Value (fabs)

fabs() is the floating-point counterpart to abs(), which only works on integers; using abs() on a double silently truncates it before taking the absolute value, a common source of subtle bugs.

Use fabs() whenever you're comparing floating-point differences against a tolerance.

Example: Absolute Value (fabs)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%.1f", fabs(-7.5));
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting #include <math.h>, so sqrt and pow are not declared.
  2. Forgetting to link with -lm on some systems, causing undefined reference errors.
  3. Calling sqrt on a negative number, which returns NaN.
Chapter Summary
  • Functions group reusable code, take parameters, and send results back with return values.
  • Prototypes declare a function before it is used, and recursion lets a function call itself.
  • Scope, lifetime, and storage classes decide where variables are visible and how long they last.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.