C Math Functions
C math functions are ready-made functions for common numeric work such as square roots, powers, rounding, remainders, and trigonometry. They matter because real programs often need more than addition and multiplication: distances, averages, angles, prices, measurements, and scientific formulas all depend on reliable math library functions.
Most C math functions are declared in <math.h> and work primarily with floating-point values. Using them well means knowing their return types, how integer values are converted, and how to compile programs that call the math library.
Overview: How C Math Functions Work
The C standard library separates basic operators from mathematical functions. Operators such as +, -, *, and / are built into the language. Functions such as sqrt, pow, sin, ceil, and fmod are declared in <math.h> and implemented by the C library.
Most commonly used math functions take and return double. A double is a floating-point type that stores an approximation of a real number. When you pass an int to a function such as sqrt, C converts it to double before the call. The result is also usually a double, even when the mathematical answer is a whole number.
This is different from integer arithmetic. The expression 17 / 5 produces 3 because both operands are integers. But 17.0 / 5.0 produces 3.4. If that value is then passed to ceil, it can round up to 4. Many math-function bugs come from doing integer arithmetic before calling the function.
On many systems you compile math programs with the math library linked explicitly, for example gcc program.c -lm. The header <math.h> gives the compiler the function declarations; the -lm option tells the linker where to find the function implementations.
Syntax
Include <math.h>, call the function with arguments of the proper type, and store or print the returned value.
double value = 25.0;
double root = sqrt(value);
double power = pow(2.0, 8.0);
printf("%.1f %.1f\n", root, power);
| Part | Meaning |
|---|---|
#include <math.h> |
Makes math function declarations visible to the compiler. |
sqrt(value) |
Calls a function and passes one argument. |
pow(2.0, 8.0) |
Calls a function with two arguments: base and exponent. |
double root |
Stores the returned floating-point result. |
%.1f |
Prints a floating-point value with one digit after the decimal point. |
Common Math Functions
| Function | Purpose | Example |
|---|---|---|
sqrt(x) |
Square root | sqrt(9.0) gives 3.0 |
pow(x, y) |
Raise x to power y |
pow(2.0, 3.0) gives 8.0 |
floor(x) |
Round down | floor(3.9) gives 3.0 |
ceil(x) |
Round up | ceil(3.1) gives 4.0 |
round(x) |
Round to nearest whole value | round(3.6) gives 4.0 |
fabs(x) |
Floating-point absolute value | fabs(-2.5) gives 2.5 |
fmod(x, y) |
Floating-point remainder | fmod(7.5, 2.0) gives 1.5 |
sin, cos, tan |
Trigonometry using radians | sin(angle) |
Examples
Basic Roots, Powers, And Rounding
#include <stdio.h>
#include <math.h>
int main(void)
{
double root = sqrt(9.0);
double square = pow(5.0, 2.0);
double rounded = round(3.6);
double down = floor(3.6);
double up = ceil(3.1);
printf("sqrt(9.0) = %.1f\n", root);
printf("pow(5.0, 2.0) = %.1f\n", square);
printf("round(3.6) = %.1f\n", rounded);
printf("floor(3.6) = %.1f\n", down);
printf("ceil(3.1) = %.1f\n", up);
return 0;
}
Output:
sqrt(9.0) = 3.0
pow(5.0, 2.0) = 25.0
round(3.6) = 4.0
floor(3.6) = 3.0
ceil(3.1) = 4.0
This program shows the functions beginners use most often. Notice that the results are printed as floating-point values. Even when sqrt(9.0) is mathematically 3, the function returns a double, so printing it with %f is correct.
Calculating Distance With hypot And fabs
#include <stdio.h>
#include <math.h>
int main(void)
{
double x1 = 2.0;
double y1 = 3.0;
double x2 = 8.0;
double y2 = 11.0;
double dx = x2 - x1;
double dy = y2 - y1;
double distance = hypot(dx, dy);
printf("Horizontal change: %.1f\n", fabs(dx));
printf("Vertical change: %.1f\n", fabs(dy));
printf("Distance: %.1f\n", distance);
return 0;
}
Output:
Horizontal change: 6.0
Vertical change: 8.0
Distance: 10.0
The function hypot(dx, dy) calculates the square root of dx * dx + dy * dy in a numerically careful way. fabs returns the absolute value of a double, which is useful when you care about the size of a change rather than its direction.
Using Trigonometry In Radians
#include <stdio.h>
#include <math.h>
int main(void)
{
double degrees = 30.0;
double pi = acos(-1.0);
double radians = degrees * pi / 180.0;
printf("Angle: %.1f degrees\n", degrees);
printf("Radians: %.3f\n", radians);
printf("sin: %.3f\n", sin(radians));
printf("cos: %.3f\n", cos(radians));
printf("tan: %.3f\n", tan(radians));
return 0;
}
Output:
Angle: 30.0 degrees
Radians: 0.524
sin: 0.500
cos: 0.866
tan: 0.577
C trigonometry functions use radians, not degrees. This example computes pi with acos(-1.0), converts degrees to radians, then calls sin, cos, and tan. The formatted output rounds the displayed values to three decimal places.
How It Works Step By Step
When the compiler sees a call such as sqrt(9.0), it checks the declaration from <math.h>. That declaration tells the compiler the function expects a double and returns a double. If you pass an int, the value is converted before the function call.
At run time, the argument value is placed where the platform’s calling convention expects it, often in a floating-point register. The math library function executes an algorithm or uses processor instructions to compute the result. The returned double is then used by the surrounding expression, assigned to a variable, or passed directly to another function such as printf.
Floating-point numbers are approximate. Values such as 0.1 cannot always be represented exactly in binary, so tiny rounding differences are normal. For user-facing output, choose an appropriate precision with format specifiers such as %.2f or %.3f instead of printing every stored digit.
Common Mistakes
Doing Integer Division Before Calling A Math Function
#include <stdio.h>
#include <math.h>
int main(void)
{
int students = 17;
int seats_per_table = 5;
double wrong = ceil(students / seats_per_table);
double correct = ceil((double) students / seats_per_table);
printf("Wrong table count: %.0f\n", wrong);
printf("Correct table count: %.0f\n", correct);
return 0;
}
Output:
Wrong table count: 3
Correct table count: 4
The first calculation divides two integers, so 17 / 5 becomes 3 before ceil ever sees it. The corrected version casts one operand to double, producing 3.4, which ceil rounds up to 4.
Using The Wrong Absolute Value Function
Use abs for int values and fabs for floating-point values. If you want the absolute value of a double, fabs communicates the correct type and avoids accidental conversion to an integer.
Forgetting That Trigonometry Uses Radians
sin(30.0) does not mean the sine of 30 degrees. It means the sine of 30 radians. Convert degrees with degrees * pi / 180.0 before calling trigonometry functions.
Forgetting To Link The Math Library
If your compiler reports an undefined reference to a math function, your source may be correct but the linker command may be incomplete. With GCC and Clang on many Unix-like systems, compile with -lm after the source file name.
Best Practices
- Include
<math.h>whenever you call standard math functions. - Use
doublefor general-purpose floating-point math unless you have a clear reason to choose another type. - Force floating-point division with a cast or a decimal literal when a fractional result matters.
- Use
fabsfor floating-point absolute values andabsfor integers. - Remember that trigonometry functions use radians.
- Format floating-point output intentionally, such as
%.2ffor money-like display. - Prefer
hypot(x, y)over manually writingsqrt(x * x + y * y)for distances when available. - Check function requirements for special inputs, such as negative values to
sqrtor zero divisors tofmod.
Practice Exercises
- Write a program that reads or stores a circle radius and prints the area using
acos(-1.0)for pi andpow(radius, 2.0)for the squared radius. - Write a program that stores two points, calculates the distance between them with
hypot, and prints the result to two decimal places. - Write a program that stores an angle in degrees, converts it to radians, and prints its sine and cosine.
Summary
- C math functions are declared in
<math.h>and usually work withdoublevalues. - Common functions include
sqrt,pow,floor,ceil,round,fabs,fmod, and trigonometry functions. - Integer division happens before a function call unless you force floating-point arithmetic.
- Trigonometry functions use radians, so convert degrees before calling them.
- On many systems, math programs must be linked with the math library using
-lm.
