The pow() function raises a base to an exponent—it computes baseexponent. Use it for squares, cubes, compound growth, square roots via fractional exponents, and any general power calculation in C.
01
base^exp
Power.
02
math.h
Link -lm.
03
pow(2,3)=8
Classic.
04
Fractional
Roots.
05
vs exp
e^x.
06
Float result
Rounding.
Fundamentals
Definition and Usage
Exponentiation repeats multiplication: 2³ = 2 × 2 × 2 = 8. pow(2.0, 3.0) computes that in one call.
Both arguments are double. The result is also double, so expect normal floating-point rounding. For integer powers of small integers, a loop may be exact—but pow handles fractional and negative exponents too.
💡
Beginner Tip
For e to the x, use exp(x) instead of pow(M_E, x). For square roots, sqrt(x) is clearer than pow(x, 0.5). Use pow when the base is not e or the exponent is not 0.5.
Foundation
📝 Syntax
Standard C declaration:
C
double pow(double base, double exponent);
Related variants
C
float powf(float base, float exponent); /* <math.h> */
long double powl(long double base, long double exponent); /* <math.h> */
Parameters
base — the number being raised to a power.
exponent — the power to raise the base to.
Return Value
baseexponent as double.
pow(2.0, 3.0) returns 8.0.
pow(x, 0.0) returns 1.0 for any x, including 0.
Negative base with non-integer exponent → NaN (not a real result).
Headers and linking
#include <math.h>
Compile: gcc pow.c -std=c11 -o pow -lm
Large results may overflow to INFINITY; check with isinf() if needed.
Cheat Sheet
⚡ Quick Reference
Call
Meaning
Result
pow(2, 3)
2³
8
pow(10, 2)
10²
100
pow(9, 0.5)
√9
3
pow(2, -2)
1 / 2²
0.25
pow(x, 0)
Any base&sup0;
1
General
pow(b, e)
b^e
Natural
exp(x)
e^x
Square root
sqrt(x)
x^0.5
Inverse
log10(pow(10,t))
= t
Hands-On
Examples Gallery
Compile every example with gcc file.c -std=c11 -o out -lm. pow is the go-to function when you need a general exponent, not just squares or natural exponentials.
📚 Getting Started
Raise a base to an integer exponent.
Example 1 — 2 Raised to the Power 3
Classic example from the reference: pow(2.0, 3.0).
Library uses log/exp or specialized algorithms internally.
Compute
3
Return result
double result; may round slightly.
Output
=
🔢
Exponentiation
Pair with log / log10 to solve for exponents.
Important
📝 Notes
Results are floating-point—small rounding errors are normal (e.g. pow(2, 10) may not print exactly 1024 in all formats).
pow(x, 0) returns 1.0 even when x is 0 (per C standard).
Negative base + non-integer exponent → NaN.
pow(0, negative) → infinity (division by zero).
Prefer exp, sqrt, cbrt when they match your need.
Link with -lm on GCC/Clang Unix-like builds.
Performance
⚡ Optimization
pow() is slower than specialized functions like exp or sqrt because it handles the general case. For small integer exponents, a simple loop (result *= base) can be faster. For squaring, x * x beats pow(x, 2). Profile hot paths before micro-optimizing.
Wrap Up
Conclusion
pow() raises any base to any exponent in one call. It covers integer powers, fractional roots, negative exponents, and real-world formulas like compound growth.
Continue with round() for rounding results, or review exp() when the base is Euler’s number.
Cast integer exponents to double explicitly if needed
Check for overflow with very large exponents
Include <math.h> and link -lm
❌ Don’t
Use pow for simple squaring—use x * x
Expect exact integers from pow on large powers
Call pow(negative, 0.5) expecting a real result
Confuse pow(2, x) with exp(x)
Ignore NaN/INF from invalid inputs
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about pow()
Exponentiation in C, explained simply.
5
Core concepts
🔢01
base^exp
Power.
Basics
📚02
pow(2,3)=8
Classic.
Example
📈03
0.5 exp
Sqrt.
Fraction
📄04
vs exp
e^x.
Compare
🔄05
Float
Rounding.
Caveat
❓ Frequently Asked Questions
pow(base, exponent) returns base raised to the power exponent — mathematically base^exponent. For example, pow(2.0, 3.0) is 8.0 because 2^3 = 8.
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double pow(double base, double exponent).
Yes. pow(x, 0.5) computes the square root of positive x. For clarity and speed, prefer sqrt(x) when you only need a square root.
pow(base, exp) works with any base. exp(x) is specialized for e^x (Euler's number). Use exp(x) instead of pow(M_E, x) for natural exponentials.
pow(negative, integer exponent) works (e.g. pow(-2, 3) is -8). pow(negative, non-integer exponent) returns NaN because the result is not a real number (e.g. pow(-4, 0.5)).
powf(base, exp) uses float types. powl(base, exp) uses long double types. Use the variant matching your variable type.
Did you know?
Computer hardware often lacks a direct “power” instruction, so pow typically uses exp(exponent * log(base)) internally for general cases. That is why specialized functions like exp and sqrt can be faster when they fit your formula.