C Math exp() Function

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

What You’ll Learn

The exp() function computes ex—Euler’s number e (about 2.718) raised to a power. It models continuous growth and decay, powers hyperbolic functions like cosh(), and pairs with log() as its inverse.

01

e^x

Natural exp.

02

math.h

Link with -lm.

03

exp(0)=1

Identity.

04

exp(1)=e

Base constant.

05

Inverse

log().

06

vs pow

Prefer exp.

Definition and Usage

The natural exponential function is written ex in mathematics. In C, exp(x) computes this value, where e ≈ 2.718281828 is Euler’s number—the unique base where the slope of the curve ex at zero equals 1.

Positive x gives growth (values rise quickly). Negative x gives decay (values shrink toward zero). At x = 0, any number to the power 0 is 1, so exp(0) = 1.

💡
Beginner Tip

Need e2? Write exp(2.0), not pow(2.718, 2). The exp function is designed for this and is more accurate than approximating e yourself.

📝 Syntax

Standard C declaration:

C
double exp(double x);

Related variants

C
float expf(float x);           /* <math.h> */
long double expl(long double x); /* <math.h> */

Parameters

  • x — the exponent; any real number.

Return Value

  • ex as a double.
  • exp(0) returns 1.0.
  • exp(1) returns e (about 2.71828).
  • Very large x may return INFINITY; very negative x may underflow to 0.0.

Headers and linking

  • #include <math.h>
  • Compile: gcc exp.c -std=c11 -o exp -lm
  • M_E (value of e) is available in <math.h> on most platforms.

⚡ Quick Reference

CallMeaningResult (approx.)
exp(0)e&sup0;1.0
exp(1)e¹ = e2.71828
exp(2)7.38906
exp(-1)1/e (decay)0.36788
log(exp(x))Inverse pairx
Natural exp
exp(x)

e^x

Inverse
log(x)

Natural log

General pow
pow(a, x)

Any base

Constant
M_E

Value of e

Examples Gallery

Compile every example with gcc file.c -std=c11 -o out -lm. The exponential function appears in finance, physics, and probability.

📚 Getting Started

Calculate e raised to a power and print the result.

Example 1 — Calculate e²

Compute exp(2) and display the result.

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

int main(void) {
    double x = 2.0;
    double result = exp(x);

    printf("e^%.2f = %.4f\n", x, result);

    return 0;
}

How It Works

exp(2) multiplies e by itself once: e × e ≈ 7.389.

Example 2 — exp(0) Equals 1

Any nonzero base to the power 0 is 1.

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

int main(void) {
    printf("exp(0) = %.1f\n", exp(0.0));
    printf("exp(1) = %.5f  (this is e)\n", exp(1.0));

    return 0;
}

How It Works

exp(1) is how you get the value of e in C when you need the constant itself.

📈 Practical Patterns

Decay, continuous growth, and the log inverse.

Example 3 — Exponential Decay with Negative Exponent

exp(-x) models quantities that shrink over time.

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

int main(void) {
    double x = 1.0;

    printf("exp(%.1f)  = %.5f  (growth)\n",  x, exp(x));
    printf("exp(%.1f) = %.5f  (decay, 1/e)\n", -x, exp(-x));

    return 0;
}

How It Works

exp(-1) equals 1/e ≈ 0.368. Radioactive decay and capacitor discharge follow this pattern.

Example 4 — Continuous Compound Interest

Future value with continuous compounding: P × ert.

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

int main(void) {
    double principal = 1000.0;
    double rate = 0.05;   /* 5% annual */
    double years = 10.0;
    double future = principal * exp(rate * years);

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

    return 0;
}

How It Works

$1000 at 5% compounded continuously for 10 years grows to about $1648.72. The formula uses exp(rate * time).

Example 5 — exp() and log() Are Inverses

Verify that log(exp(x)) returns the original value.

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

int main(void) {
    double values[] = { 0.0, 1.0, 2.5, -1.5 };
    size_t i;

    printf("   x     exp(x)    log(exp(x))\n");
    for (i = 0; i < 4; i++) {
        double x = values[i];
        printf("%5.1f  %8.4f  %12.4f\n", x, exp(x), log(exp(x)));
    }

    return 0;
}

How It Works

log undoes exp. When you need to solve ex = y for x, use x = log(y).

🚀 Common Use Cases

  • Finance — continuous compound interest and discounting.
  • Physics — radioactive decay, capacitor charging, population growth.
  • Probability — softmax, Poisson distribution, entropy calculations.
  • Machine learning — sigmoid and log-softmax use exp internally.
  • Hyperbolic functionscosh(x) and sinh(x) are built from exp.

🧠 How exp() Works

1

Receive exponent x

Any real number—positive, negative, or zero.

Input
2

Compute e^x

Library uses range reduction and polynomial approximation.

Compute
3

Return double

Always positive (except underflow to 0). Never negative.

Output
=

Exponential value

Use log(y) to reverse when y > 0.

📝 Notes

  • Computes ex, not 10x (use pow(10, x) or exp(x * log(10)) for base 10).
  • exp(0) = 1; exp(1) = e.
  • Result is always positive; exp(x) > 0 for all finite x.
  • Large positive x → overflow (INFINITY); large negative x → underflow (0).
  • Prefer exp(x) over pow(M_E, x) for accuracy and speed.
  • Inverse: log(exp(x)) == x for finite x.

⚡ Optimization

exp() is heavily optimized in modern math libraries. If you call it repeatedly with the same exponent in a loop, cache the result in a variable. For softmax-style code summing many exponentials, use the log-sum-exp trick to avoid overflow—subtract the maximum value before calling exp.

Conclusion

exp() raises Euler’s number to a power, modeling growth and decay across science and finance. Remember exp(0) = 1, use log() as its inverse, and prefer exp over manual pow for ex.

Continue with fabs() for absolute values, or explore log() for natural logarithms.

💡 Best Practices

✅ Do

  • Include <math.h> and link with -lm
  • Use exp(x) for e^x
  • Use log() to solve e^x = y
  • Check isfinite() for extreme inputs
  • Cache repeated exp calls with the same argument

❌ Don’t

  • Use pow(2.718, x) instead of exp(x)
  • Confuse exp with exp2 (which computes 2^x)
  • Expect negative results from exp
  • Call log() on zero or negative values
  • Ignore overflow in softmax-style summations

Key Takeaways

Knowledge Unlocked

Five things to remember about exp()

Natural exponential in C, explained simply.

5
Core concepts
📚 02

exp(0)=1

Identity.

Rule
📈 03

Always +

> 0 output.

Range
📄 04

Inverse

log().

Pair
🔄 05

vs pow

Prefer exp.

Tip

❓ Frequently Asked Questions

exp(x) returns e raised to the power x, where e is Euler's number (about 2.71828). For example, exp(0) is 1.0, exp(1) is about 2.718, and exp(2) is about 7.389.
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double exp(double x).
e (Euler's number) is the mathematical constant approximately 2.718281828. It is the base of the natural logarithm. exp(1) equals e exactly (within floating-point precision).
exp(x) computes e^x. log(x) computes the natural logarithm ln(x)—the inverse operation. log(exp(x)) equals x, and exp(log(x)) equals x for positive x.
Prefer exp(x) for e^x. It is typically faster and more accurate than pow(M_E, x). Use pow only when the base is not e.
Very large positive x may overflow to +INFINITY. Very large negative x approaches 0 (underflow). Check with isinf() or isfinite() if inputs can be extreme.
Did you know?

The constant e appears in the formula for continuously compounded interest: A = P · ert. As compounding intervals shrink toward zero, discrete compound interest approaches this exponential form—which is exactly what exp() computes.

Explore C Math Functions

Continue with fabs() 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