Course contentsShow
← C
Lesson 863 of 1,21912. The Standard LibraryFree lesson

Trigonometric functions in radians

Trigonometric functions in radians

What you'll learn: How to use the sin, cos, and tan functions from the math library and why you must convert degrees to radians first.

The Language of the math.h Library

In high school geometry, we often think of angles in degrees, like 90° for a right angle or 180° for a straight line. However, C’s <math.h> library speaks a different language: radians.

If you try to calculate the sine of 90 degrees by writing sin(90), C won't give you 1.0. Instead, it will treat 90 as a value in radians (which is about 14 full circles!) and give you a mathematically correct, but unexpected, result. To get the right answer, you have to translate your degrees into radians before handing them to the function.

The Conversion Formula

Think of the conversion like changing currency. The "exchange rate" is based on the constant PI (π\pi).

  • 180 degrees = π\pi radians.
  • Formula: radians = degrees * (PI / 180.0)

Since standard C doesn't always provide a built-in constant for PI, it is common practice to define it yourself as 3.14159265.

Coding Sine and Cosine

To use these functions, you must include <math.h> at the top of your file. On many systems (like Linux), you also need to tell the compiler to link the math library by adding -lm to your compile command.

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

int main() {
    double degrees = 45.0;
    double pi = 3.1415926535;
    
    // Step 1: Convert degrees to radians
    double radians = degrees * (pi / 180.0);
    
    // Step 2: Use the trig functions
    double sine_value = sin(radians);
    double cosine_value = cos(radians);
    
    printf("Sine of 45 degrees: %f\n", sine_value);
    printf("Cosine of 45 degrees: %f\n", cosine_value);
    
    return 0;
}

Precision and Data Types

Notice that we use double instead of int or float. Trigonometric functions are very precise, and they almost always return fractional values between -1 and 1. Using double ensures that you don't lose the tiny decimals that make your calculations accurate, whether you are building a physics engine for a game or a navigation system.

Key takeaway

C's trigonometric functions require angles in radians, so always multiply degrees by (PI / 180.0) before passing them to sin(), cos(), or tan().