C pow() Function

Definition
In C, pow() returns the base raised to an exponent. pow(2, 3) is 8.
You pass two double values, the base and the exponent, and both are required. The result is a double. Include <math.h>.
Syntax
Pass the base, then the exponent. Start with this call.
| Call | What it does |
|---|
pow(2.0, 3.0) | Returns 8, because 2 × 2 × 2 is 8. |
Include <math.h>. On GCC, link with -lm.
Library declarations
double pow(double x, double y);
float powf(float x, float y);
long double powl(long double x, long double y);
Parameters
| Parameter | Description |
|---|
| base | Required. The number you raise. |
| exponent | Required. The power you raise it to. |
Return Value
| Return Value | Description |
|---|
| base to the exponent | A double. pow(2, 3) is 8. |
| NaN | Possible when a negative base is raised to a fractional exponent. |
C Examples
Example 1 — Common Powers
Compute a few everyday powers with integer exponents.
#include <math.h>
#include <stdio.h>
int main(void) {
printf("pow(2, 3) = %.1f\n", pow(2.0, 3.0));
printf("pow(5, 2) = %.1f\n", pow(5.0, 2.0));
printf("pow(10, 0) = %.1f\n", pow(10.0, 0.0));
return 0;
}
pow(2, 3) = 8.0
pow(5, 2) = 25.0
pow(10, 0) = 1.0
How It Works
- Include math.h. Declares
pow; link with -lm on GCC. - Cube and square.
2³ = 8 and 5² = 25. - Any nonzero base to 0.
pow(10, 0) is 1. - Double results. Even whole answers come back as
double.
Example 2 — Fractional and Negative Exponents
Fractional exponents act like roots. Negative exponents give reciprocals. Prefer sqrt when you only need a square root.
#include <math.h>
#include <stdio.h>
int main(void) {
printf("pow(16, 0.5) = %.1f\n", pow(16.0, 0.5));
printf("pow(8, 1.0/3.0) ≈ %.6f\n", pow(8.0, 1.0 / 3.0));
printf("pow(2, -1) = %.1f\n", pow(2.0, -1.0));
return 0;
}
pow(16, 0.5) = 4.0
pow(8, 1.0/3.0) ≈ 2.000000
pow(2, -1) = 0.5
How It Works
- Half power.
pow(16, 0.5) is √16 = 4. - One-third power. Approximates a cube root; prefer
cbrt for negatives. - Negative exponent.
pow(2, -1) is 1 / 2. - Float noise. Root results may print with tiny rounding error; that is normal for
double.
Example 3 — Compound Growth
Use pow in the formula A = P × (1 + r)n.
#include <math.h>
#include <stdio.h>
int main(void) {
double principal = 1000.0;
double rate = 0.05;
int years = 3;
double amount = principal * pow(1.0 + rate, years);
printf("principal = %.2f\n", principal);
printf("after %d years = %.2f\n", years, amount);
return 0;
}
principal = 1000.00
after 3 years = 1157.63
How It Works
- Growth factor. Each year multiplies by
1.05. - Raise to years.
pow(1.05, 3) applies that factor three times. - Scale by principal. Multiply by
1000 for the final amount. - Money note. This is a math demo; real finance often uses cents or decimal types.
Frequently Asked Questions
pow() returns the base raised to an exponent. pow(2, 3) is 8.
Both are required. base is the number you raise, and exponent is the power.
A double. A negative base with a fractional exponent can return NaN.
Include <math.h>. On GCC, link with -lm.
Use sqrt() for square roots and cbrt() for cube roots, especially when the input can be negative.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful