C tan() Function

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

Definition

In C, tan() returns the tangent of an angle. The angle is in radians. tan(0) is 0.

You pass one double. The result is a double. Include <math.h>.

Syntax

Pass the angle in radians. Start with this call.

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

Include <math.h>. On GCC, link with -lm. Convert degrees with rad = deg * acos(-1.0) / 180.0.

Library declarations
C
double tan(double x);
float tanf(float x);
long double tanl(long double x);

Parameters

ParameterDescription
xRequired. The angle in radians.

Return Value

Return ValueDescription
TangentA double. tan(0) is 0.

C Examples

Example 1 — Common Radian Values

Use a portable π and print classic tangent results.

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

int main(void) {
    const double PI = acos(-1.0);

    printf("tan(0)    = %.4f\n", tan(0.0));
    printf("tan(PI/6) = %.4f\n", tan(PI / 6.0));
    printf("tan(PI/4) = %.4f\n", tan(PI / 4.0));

    return 0;
}

How It Works

  1. Get π. acos(-1.0) is a portable constant for π.
  2. Pass radians. PI/6 is 30°; PI/4 is 45°.
  3. Known results. tan(π/4) = 1; tan(π/6) ≈ 1/√3.
  4. Link libm. On GCC, remember -lm.

Example 2 — Convert Degrees Before tan

Never pass degree numbers directly—convert first.

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

int main(void) {
    double deg = 45.0;
    double pi = acos(-1.0);
    double rad = deg * pi / 180.0;

    printf("%.0f deg = %.4f rad\n", deg, rad);
    printf("tan(45 deg) = %.4f\n", tan(rad));

    return 0;
}

How It Works

  1. Start with degrees. 45° is a familiar angle.
  2. Scale to radians. Multiply by π / 180.
  3. Call tan. Result is 1.
  4. Avoid the trap. tan(45) without conversion is 45 radians.

Example 3 — Check sin / cos

Confirm tan(x) matches sin(x) / cos(x).

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

int main(void) {
    double rad = acos(-1.0) / 6.0;  /* 30° */
    double t = tan(rad);
    double ratio = sin(rad) / cos(rad);

    printf("tan     = %.5f\n", t);
    printf("sin/cos = %.5f\n", ratio);

    return 0;
}

How It Works

  1. Pick a safe angle. 30° is far from 90°, so cosine is not zero.
  2. Call tan. Library result ≈ 0.57735.
  3. Divide sin by cos. Same value—identity check.
  4. Prefer tan. Clearer and usually more accurate than dividing yourself.

Frequently Asked Questions

tan() returns the tangent of an angle in radians. tan(0) is 0.
x is required. It is the angle in radians.
A double. Near π/2 the value is not a normal finite result.
Include <math.h>. On GCC, link with -lm.
No. Convert degrees to radians first. 45 degrees is π/4 radians.

Next: tanh()

Learn the hyperbolic tangent—similar name, different math.

tanh() 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