C tanh() Function

Definition
In C, tanh() returns the hyperbolic tangent of x. It is not an angle. tanh(0) is 0, and the result stays between -1 and 1.
You pass one double. The result is a double with the same sign as x. Include <math.h>.
Syntax
Pass any real number. Start with this call.
| Call | What it does |
|---|
tanh(0.0) | Returns 0. |
Include <math.h>. On GCC, link with -lm. x is not an angle in degrees.
Library declarations
double tanh(double x);
float tanhf(float x);
long double tanhl(long double x);
Parameters
| Parameter | Description |
|---|
| x | Required. Any real double. It is not an angle. |
Return Value
| Return Value | Description |
|---|
| Hyperbolic tangent | A double between -1 and 1, with the same sign as x. |
C Examples
Example 1 — Zero and Odd Property
Show tanh(0) and that the function is odd.
#include <math.h>
#include <stdio.h>
int main(void) {
printf("tanh(0) = %.4f\n", tanh(0.0));
printf("tanh(0.5) = %.4f\n", tanh(0.5));
printf("tanh(-0.5) = %.4f\n", tanh(-0.5));
return 0;
}
tanh(0) = 0.0000
tanh(0.5) = 0.4621
tanh(-0.5) = -0.4621
How It Works
- Zero in.
tanh(0) is exactly 0. - Positive half.
tanh(0.5) ≈ 0.4621. - Odd function. Negating the input flips the sign of the result.
- Link libm. On GCC, remember
-lm.
Example 2 — Large Inputs Saturate
Big |x| pushes the result toward ±1, never past it.
#include <math.h>
#include <stdio.h>
int main(void) {
printf("tanh(1) = %.6f\n", tanh(1.0));
printf("tanh(5) = %.6f\n", tanh(5.0));
printf("tanh(-5) = %.6f\n", tanh(-5.0));
return 0;
}
tanh(1) = 0.761594
tanh(5) = 0.999909
tanh(-5) = -0.999909
How It Works
- Moderate input.
tanh(1) is about 0.76. - Large positive.
tanh(5) is almost 1. - Large negative. Same magnitude, opposite sign—near
−1. - Bounded. Unlike
tan, output never escapes (−1, 1).
Example 3 — Check sinh / cosh
Confirm tanh(x) matches sinh(x) / cosh(x).
#include <math.h>
#include <stdio.h>
int main(void) {
double x = 0.75;
double t = tanh(x);
double ratio = sinh(x) / cosh(x);
printf("tanh = %.6f\n", t);
printf("sinh/cosh = %.6f\n", ratio);
return 0;
}
tanh = 0.635149
sinh/cosh = 0.635149
How It Works
- Pick any real x. Here
0.75—no unit conversion. - Call tanh. Library result ≈
0.635149. - Divide sinh by cosh. Same value—identity check.
- Prefer tanh. Clearer and usually safer than dividing yourself.
Frequently Asked Questions
tanh() returns the hyperbolic tangent of x. tanh(0) is 0, and the result stays between -1 and 1.
x is required. Any real double is allowed. It is not an angle.
A double between -1 and 1, with the same sign as x.
Include <math.h>. On GCC, link with -lm.
No. tan() takes an angle in radians. tanh() is the hyperbolic tangent.
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