C tanh() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

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.

C
tanh(0.0);
CallWhat it does
tanh(0.0)Returns 0.

Include <math.h>. On GCC, link with -lm. x is not an angle in degrees.

Library declarations
C
double tanh(double x);
float tanhf(float x);
long double tanhl(long double x);

Parameters

ParameterDescription
xRequired. Any real double. It is not an angle.

Return Value

Return ValueDescription
Hyperbolic tangentA 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.

C
#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;
}

How It Works

  1. Zero in. tanh(0) is exactly 0.
  2. Positive half. tanh(0.5) ≈ 0.4621.
  3. Odd function. Negating the input flips the sign of the result.
  4. Link libm. On GCC, remember -lm.

Example 2 — Large Inputs Saturate

Big |x| pushes the result toward ±1, never past it.

C
#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;
}

How It Works

  1. Moderate input. tanh(1) is about 0.76.
  2. Large positive. tanh(5) is almost 1.
  3. Large negative. Same magnitude, opposite sign—near −1.
  4. Bounded. Unlike tan, output never escapes (−1, 1).

Example 3 — Check sinh / cosh

Confirm tanh(x) matches sinh(x) / cosh(x).

C
#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;
}

How It Works

  1. Pick any real x. Here 0.75—no unit conversion.
  2. Call tanh. Library result ≈ 0.635149.
  3. Divide sinh by cosh. Same value—identity check.
  4. 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.

Next: trunc()

Learn how to chop the fractional part toward zero.

trunc() tutorial →

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.

9 people found this page helpful