C exp() Function

Definition
In C, exp() returns e raised to the power x. exp(0) is 1, and exp(1) is about 2.718.
You pass one double. The result is a positive double. Include <math.h>.
Syntax
Pass the exponent. Start with this call.
| Call | What it does |
|---|
exp(1.0) | Returns e, about 2.718. |
Include <math.h>. On GCC, link with -lm.
Library declarations
double exp(double x);
float expf(float x);
long double expl(long double x);
Parameters
| Parameter | Description |
|---|
| x | Required. The exponent. |
Return Value
| Return Value | Description |
|---|
| e to the power x | A positive double. exp(0) is 1. |
C Examples
Example 1 — Basic Exponential Values
Print exp(0), exp(1), and exp(2).
#include <stdio.h>
#include <math.h>
int main(void) {
printf("exp(0) = %.6f\n", exp(0.0));
printf("exp(1) = %.6f\n", exp(1.0));
printf("exp(2) = %.6f\n", exp(2.0));
return 0;
}
exp(0) = 1.000000
exp(1) = 2.718282
exp(2) = 7.389056
How It Works
- Include math.h. That declares
exp. - exp(0) is 1. Any nonzero base to the power 0 is 1.
- exp(1) is e. This is how you get Euler’s number in code.
- exp(2) is e². Growth compounds as the exponent rises.
Example 2 — Negative Exponent (Decay)
Negative exponents give values between 0 and 1—useful for decay and damping models.
#include <stdio.h>
#include <math.h>
int main(void) {
double y = exp(-1.0);
printf("exp(-1) = %.6f\n", y);
printf("1/e = %.6f\n", 1.0 / exp(1.0));
return 0;
}
exp(-1) = 0.367879
1/e = 0.367879
How It Works
- Negative power.
exp(-1) means 1/e. - Cross-check. Dividing 1 by
exp(1) matches. - Always positive. For finite real
x, exp(x) stays > 0. - Decay pattern. As
x goes more negative, the result approaches 0.
Example 3 — Inverse With log()
Natural log undoes exp (within floating-point error).
#include <stdio.h>
#include <math.h>
int main(void) {
double x = 2.5;
double y = exp(x);
double back = log(y);
printf("x = %.6f\n", x);
printf("exp(x) = %.6f\n", y);
printf("log(exp(x))= %.6f\n", back);
return 0;
}
x = 2.500000
exp(x) = 12.182494
log(exp(x))= 2.500000
How It Works
- Raise e.
exp(2.5) grows to about 12.18. - Take ln.
log(y) is the natural logarithm. - Recover x.
log(exp(x)) returns to 2.5. - Require y > 0.
log is only defined for positive arguments.
Frequently Asked Questions
exp() returns e raised to the power x. exp(0) is 1, and exp(1) is about 2.718.
x is required. It is the exponent.
A positive double. exp(0) is 1.
Include <math.h>. On GCC, link with -lm.
A large positive x overflows to infinity. A large negative x underflows toward 0.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
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