C Math pow() Function

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
<math.h>

What You’ll Learn

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.

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.

📝 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.

⚡ Quick Reference

CallMeaningResult
pow(2, 3)8
pow(10, 2)10²100
pow(9, 0.5)√93
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

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).

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double base = 2.0;
    double exponent = 3.0;
    double result = pow(base, exponent);

    printf("%.0f ^ %.0f = %.0f\n", base, exponent, result);

    return 0;
}

How It Works

2 × 2 × 2 = 8. pow handles the repeated multiplication internally.

Example 2 — Square Root via pow(x, 0.5)

A fractional exponent of 0.5 means square root.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double x = 9.0;

    printf("x = %.0f\n", x);
    printf("pow(x, 0.5) = %.1f\n", pow(x, 0.5));
    printf("sqrt(x)     = %.1f  (preferred)\n", sqrt(x));

    return 0;
}

How It Works

x1/2 = √x. Both calls give 3, but sqrt communicates intent better.

📈 Practical Patterns

Negative exponents, finance, and comparison with exp.

Example 3 — Negative Exponent: 2−2

A negative exponent means reciprocal: 2−2 = 1 / 2² = 0.25.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double result = pow(2.0, -2.0);

    printf("2^(-2) = %.2f\n", result);
    printf("1/4    = %.2f\n", 1.0 / 4.0);

    return 0;
}

How It Works

Negative exponents flip the base into the denominator. Useful for decay and scaling down.

Example 4 — Compound Growth

Future value: principal × pow(1 + rate, years).

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double principal = 1000.0;
    double rate = 0.05;
    int years = 10;
    double future = principal * pow(1.0 + rate, (double)years);

    printf("Principal: $%.0f at %.0f%% for %d years\n",
           principal, rate * 100, years);
    printf("Future value: $%.2f\n", future);

    return 0;
}

How It Works

Each year multiplies by 1.05. Ten years means (1.05)10 ≈ 1.629, so $1000 grows to about $1629.

Example 5 — pow() vs exp() for ex

Both compute e2, but exp is the right tool.

C
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>

int main(void) {
    double x = 2.0;

    printf("x = %.1f\n", x);
    printf("exp(x)        = %.4f\n", exp(x));
    printf("pow(M_E, x)   = %.4f\n", pow(M_E, x));
    printf("pow(2.0, x)   = %.4f  (2^x, not e^x)\n", pow(2.0, x));

    return 0;
}

How It Works

exp and pow(M_E, x) both give e2 ≈ 7.389. pow(2, x) is 22 = 4—a different base entirely.

🚀 Common Use Cases

  • Polynomials and geometry — areas, volumes (e.g. r², side³).
  • Compound interestpow(1 + rate, years).
  • Scientific formulas — inverse-square laws, half-life scaling.
  • Unit conversion — powers of ten for metric prefixes.
  • Graphics — gamma correction, distance falloff curves.

🧠 How pow() Works

1

Receive base and exponent

Both as double values.

Input
2

Compute baseexp

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.

📝 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.

⚡ 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.

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.

💡 Best Practices

✅ Do

  • Use exp(x) for ex, not pow(M_E, x)
  • Use sqrt(x) for square roots when possible
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about pow()

Exponentiation in C, explained simply.

5
Core concepts
📚 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.

Explore C Math Functions

Continue with round() or browse the full math function reference.

Math Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful