C Math tan() Function

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
<math.h>

What You’ll Learn

The tan() function computes the tangent of an angle in radians—the ratio of opposite to adjacent on a right triangle, or sin/cos on the unit circle. Essential for slopes, rotations, and graphics.

01

Tangent

opp/adj.

02

math.h

Link -lm.

03

Radians

Not degrees.

04

tan(pi/4)=1

45°.

05

sin/cos

Identity.

06

vs tanh

Different.

Definition and Usage

Tangent is opposite ÷ adjacent on a right triangle. Equivalently, tan(θ) = sin(θ) / cos(θ) when cos(θ) ≠ 0.

In C, tan(x) expects x in radians. At 45° (π/4 radians), opposite equals adjacent, so tan(M_PI / 4) is 1.0.

💡
Beginner Tip

Tangent blows up near 90° and 270° where cos is zero. To find an angle from a slope, use atan2(y, x) instead of dividing and inverting manually—it handles all quadrants correctly.

📝 Syntax

Standard C declaration:

C
double tan(double x);

Related variants

C
float tanf(float x);             /* <math.h> */
long double tanl(long double x); /* <math.h> */

Parameters

  • x — angle in radians.

Return Value

  • Tangent of x as double (unbounded).
  • tan(0) returns 0.0; tan(M_PI / 4) returns 1.0.
  • Near cos(x) = 0, result may be huge or INFINITY.

Headers and linking

  • #include <math.h>
  • Compile: gcc tan.c -std=c11 -o tan -lm
  • On MSVC, define _USE_MATH_DEFINES before <math.h> for M_PI.

⚡ Quick Reference

CallAngleResult
tan(0)0
tan(M_PI / 6)30°0.577 (1/√3)
tan(M_PI / 4)45°1
tan(M_PI / 3)60°1.732 (√3)
tan(M_PI / 2)90°Undefined (∞)
Forward
tan(angle)

Radians in

Identity
sin(x)/cos(x)

cos ≠ 0

Inverse
atan2(y, x)

Angle from slope

Not this
tanh(x)

Hyperbolic

Examples Gallery

Compile every example with gcc file.c -std=c11 -o out -lm. All angles are in radians unless converted from degrees explicitly.

📚 Getting Started

Compute tangent of a 45-degree angle.

Example 1 — Tangent of 45 Degrees

Classic example from the reference: tan(M_PI / 4).

C
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>

int main(void) {
    double angle_rad = M_PI / 4.0;
    double result = tan(angle_rad);

    printf("Tangent of 45 degrees: %.6f\n", result);

    return 0;
}

How It Works

On a 45-45-90 triangle, opposite equals adjacent, so the ratio is exactly 1.

Example 2 — tan(x) = sin(x) / cos(x)

Verify the fundamental identity at 60 degrees.

C
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>

int main(void) {
    double rad = M_PI / 3.0;

    printf("angle = pi/3 (60 degrees)\n");
    printf("tan(x)     = %.6f\n", tan(rad));
    printf("sin/cos    = %.6f\n", sin(rad) / cos(rad));

    return 0;
}

How It Works

Both paths give √3 ≈ 1.732. Tangent is sine divided by cosine whenever cosine is non-zero.

📈 Practical Patterns

Degree conversion, slope angles, and comparison with tanh.

Example 3 — Tangent at Common Angles

Convert degrees to radians, then call tan().

C
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>

int main(void) {
    double degs[] = { 0.0, 30.0, 45.0, 60.0 };
    size_t i;

    printf(" deg    tan()\n");
    for (i = 0; i < 4; i++) {
        double rad = degs[i] * M_PI / 180.0;
        printf("%4.0f  %6.3f\n", degs[i], tan(rad));
    }

    return 0;
}

How It Works

Tangent increases from 0° to 60°. It would shoot to infinity at 90°—avoid that angle.

Example 4 — Angle of a Line with atan2()

Rise 3, run 4 — what angle does the line make? Use atan2, the inverse of tangent in 2D.

C
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>

int main(void) {
    double rise = 3.0;
    double run = 4.0;
    double angle_rad = atan2(rise, run);
    double angle_deg = angle_rad * 180.0 / M_PI;

    printf("Rise: %.0f, Run: %.0f\n", rise, run);
    printf("Slope tan = %.3f\n", tan(angle_rad));
    printf("Angle: %.1f degrees\n", angle_deg);

    return 0;
}

How It Works

atan2(3, 4) gives the angle whose tangent is 3/4 = 0.75. Prefer atan2 over atan(y/x) for correct quadrant handling.

Example 5 — tan() vs tanh() at the Same Input

Circular tangent vs hyperbolic tangent—completely different curves.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double vals[] = { 0.0, 0.5, 1.0, 1.5 };
    size_t i;

    printf("   x     tan(x)   tanh(x)\n");
    for (i = 0; i < 4; i++) {
        double x = vals[i];
        printf("%4.1f  %8.4f  %8.4f\n", x, tan(x), tanh(x));
    }

    return 0;
}

How It Works

Both are zero at 0, but tan grows quickly while tanh saturates toward 1. Never confuse the two in formulas.

🚀 Common Use Cases

  • Graphics and games — rotation angles, slope of lines, camera tilt.
  • Physics — incline problems, force components on ramps.
  • Engineering — roof pitch, grade percent, structural angles.
  • Navigation — bearing calculations with atan2.
  • Pair with atan2() — recover angle from rise and run.

🧠 How tan() Works

1

Receive radians

Angle x in radians (convert from degrees if needed).

Input
2

Compute sin/cos

Internally: tangent = sine divided by cosine.

Compute
3

Return tan(x)

Unbounded double; huge near 90° multiples.

Output
=

Tangent value

Use atan2(y, x) to go from slope back to angle.

📝 Notes

  • Input is radians, not degrees—convert with degrees * M_PI / 180.0.
  • tan(x) = sin(x) / cos(x) when cos(x) ≠ 0.
  • Undefined at π/2, 3π/2, … (90°, 270°, …).
  • Output is unbounded—not limited to [−1, 1] like sin/cos.
  • Do not confuse tan() with tanh() (hyperbolic).
  • Link with -lm on GCC/Clang Unix-like builds.

⚡ Optimization

tan() is implemented in the platform math library. If you already have sin and cos for the same angle, some libraries compute both together—but calling tan directly is fine unless profiling says otherwise. For angle-from-vector work, atan2 is the right inverse tool.

Conclusion

tan() computes the tangent of an angle in radians—opposite over adjacent, or sin/cos. Convert degrees first, avoid angles where cosine is zero, and use atan2 to reverse the operation in 2D.

Continue with tanh() for hyperbolic tangent, or review atan2() for full-circle angle recovery.

💡 Best Practices

✅ Do

  • Convert degrees to radians before calling tan
  • Use atan2(y, x) to get angle from coordinates
  • Check for near-90° angles where tangent diverges
  • Remember tan = sin / cos for understanding
  • Include <math.h> and link -lm

❌ Don’t

  • Pass degrees directly to tan()
  • Confuse tan() with tanh()
  • Call tan at π/2 without expecting infinity
  • Use atan(y/x) when atan2(y, x) is needed
  • Assume tangent stays between −1 and 1

Key Takeaways

Knowledge Unlocked

Five things to remember about tan()

Circular tangent in C, explained simply.

5
Core concepts
📚 02

Radians

Not degrees.

Input
📈 03

sin/cos

Identity.

Math
📄 04

90°

Undefined.

Caveat
🔄 05

atan2

Inverse 2D.

Tool

❓ Frequently Asked Questions

tan(x) returns the tangent of an angle given in radians. On a right triangle, tangent is opposite divided by adjacent. For example, tan(pi/4) is 1.0 (45 degrees).
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double tan(double x).
Radians. Convert degrees first: radians = degrees * (M_PI / 180.0). tan(45) is not the same as tan(45 degrees).
Tangent is undefined when cos(x) is zero — at pi/2, 3*pi/2, and similar angles (90°, 270°, etc.). tan may return a very large value or infinity; avoid calling tan exactly at those angles.
tan(x) equals sin(x) / cos(x) when cos(x) is not zero. All three take radians. Use this identity to verify results or understand the function.
tan() is circular tangent (radians in, unbounded output with asymptotes). tanh() is hyperbolic tangent (any real input, output in (-1, 1)). Different functions despite similar names.
Did you know?

A road grade of 100% means a rise of 1 meter per 1 meter run—a 45° angle, because tan(45°) = 1. Steeper grades have higher tangent values; vertical cliffs approach infinity.

Explore C Math Functions

Continue with tanh() or browse the full math function reference.

Math Functions Index →

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.

5 people found this page helpful