tan() Function Example in C Programming

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

tan() Function Overview

The tan() function in C is used to determine the tangent of a given angle expressed in radians. It is part of the math.h library. The function accepts a single argument— the angle in radians— and returns the tangent value of that angle. 

Key Points:

- It's imperative to include the math.h header to access the tan() function. 

- The angle provided to the function should be in radians. To convert from degrees to radians, the formula is: radian = degree × π / 180

- When compiling, always ensure to link the math library using the -lm flag.

Source Code Example

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

int main() {
    double angle_in_degrees, angle_in_radians, result;

    // Obtain angle in degrees from user
    printf("Enter angle in degrees: ");
    scanf("%lf", &angle_in_degrees);

    // Convert the angle from degrees to radians
    angle_in_radians = angle_in_degrees * M_PI / 180.0;

    // Calculate the tangent of the angle
    result = tan(angle_in_radians);

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

    return 0;


Enter angle in degrees: 45
Tangent value of 45.00 degrees is: 1.00


1. We begin by incorporating the essential header files: stdio.h for I/O operations and math.h for accessing mathematical functions like tan().

2. In our main() function, we request the user to input an angle value in degrees.

3. This angle is subsequently converted from degrees to radians using the formula: radian = degree × π / 180. Note that the constant M_PI in C, representing the value of π, is defined in math.h.

4. We then call the tan() function with the radian value to compute the tangent of the angle.

5. Lastly, we print the result to the console.