tanh() Function Example in C Programming

In this source code example, we will see how to use the tanh() function in C programming with an example.

tanh() Function Overview

The tanh() function in C computes the hyperbolic tangent of a specified number. 

The function returns values in the range (-1, 1), and is especially useful in various areas of physics, engineering, and mathematics. It is housed within the math.h library. 

Key Points: 

- To utilize the tanh() function, ensure you include the math.h header. 

- The argument to this function can be any real number. 

- While compiling, remember to link the math library using the -lm flag.

Source Code Example

#include <stdio.h>
#include <math.h>  // Necessary for tanh()

int main() {
    double value, result;

    // Prompt the user for a value
    printf("Enter a value to compute its hyperbolic tangent: ");
    scanf("%lf", &value);

    // Calculate the hyperbolic tangent of the given value
    result = tanh(value);

    // Display the result
    printf("Hyperbolic tangent of %.2lf is: %.2lf\n", value, result);

    return 0;


Enter a value to compute its hyperbolic tangent: 1
Hyperbolic tangent of 1.00 is: 0.76


1. We start off by incorporating the necessary header files: stdio.h for standard I/O operations and math.h to leverage the tanh() function.

2. In the main() function, we request a value from the user.

3. The tanh() function is then called to compute the hyperbolic tangent of the supplied value.

4. The result is subsequently displayed on the console.