Trigonometric Functions in Standard C Library

What are Trigonometric Functions in C?

Trigonometry deals with the relationships between the angles and sides of triangles. In programming, trigonometric functions help us solve real-world problems related to physics, engineering, and graphics.

C provides a set of built-in functions to perform trigonometric calculations, such as:

Function Purpose Syntax
sin(x) Sine of angle x (in radians) double sin(double x)
cos(x) Cosine of angle x double cos(double x)
tan(x) Tangent of angle x double tan(double x)
asin(x) Inverse sine (returns radians) double asin(double x)
acos(x) Inverse cosine double acos(double x)
atan(x) Inverse tangent double atan(double x)
atan2(y, x) Arc tangent of y/x double atan2(double y, double x)

All these functions are available in the math.h header file.

Including the math Library

Before you can use trigonometric functions in C, you must include the math.h header:

#include <math.h>

Also, if you are using GCC to compile, don’t forget to link the math library using the -lm flag:

advertisement
gcc program.c -o program -lm

Angle Units: Degrees vs Radians

Trigonometric functions in C expect angles to be in radians, not degrees.

To convert degrees to radians, use the formula:

radians = degrees * (M_PI / 180.0);

M_PI is a constant defined in math.h representing the value of π (approximately 3.14159).

Free 30-Day C Certification Bootcamp is Live. Join Now!

Syntax of Trigonometric Functions in C

double sin(double x);
double cos(double x);
double tan(double x);

These functions take an angle in radians and return the respective trigonometric value.

Example of Trigonometric Functions in C

#include <stdio.h>
#include <math.h>
 
int main()
{
    double angle = M_PI / 4;  // 45 degrees in radians
 
    printf("Trigonometric Evaluation:\n");
    printf("sin(Ï€/4) = %f\n", sin(angle));  // Expected ~0.7071
    printf("cos(Ï€/4) = %f\n", cos(angle));  // Expected ~0.7071
    printf("tan(Ï€/4) = %f\n", tan(angle));  // Expected ~1.0000
 
    return 0;
}

Output:

Trigonometric Evaluation:
sin(Ï€/4) = 0.707107
cos(Ï€/4) = 0.707107
tan(Ï€/4) = 1.000000

This C program calculates the sine, cosine, and tangent of a 45-degree angle (Ï€/4 radians) using functions from the math library. It prints the results: around 0.7071 for sine and cosine, and 1.0000 for tangent. The program shows how to use trigonometric functions in C with radians.

Inverse Trigonometric Functions in C

Inverse trigonometric functions return the angle in radians when given the trigonometric ratio. These functions are defined in <math.h>.

Syntax:

double asin(double x);
double acos(double x);
double atan(double x);
double atan2(double y, double x);

Example:

advertisement
#include <stdio.h>
#include <math.h>
 
int main()
{
    double quizValue = -0.7071;
    double testValue = 0.5773;
    double mcqX = -2.0, mcqY = 2.0;
 
    printf("Inverse Trigonometric Demo:\n");
 
    double quizAngle = asin(quizValue);  // Inverse sine
    double testAngle = acos(quizValue);  // Inverse cosine
    double certAngle = atan(testValue);  // Inverse tangent
    double mcqAngle = atan2(mcqY, mcqX); // Handles quadrant
 
    printf("asin(%.4f) = %.6f radians\n", quizValue, quizAngle);
    printf("acos(%.4f) = %.6f radians\n", quizValue, testAngle);
    printf("atan(%.4f) = %.6f radians\n", testValue, certAngle);
    printf("atan2(%.1f, %.1f) = %.6f radians\n", mcqY, mcqX, mcqAngle);
 
    return 0;
}

Output:

Inverse Trigonometric Demo:
asin(-0.7071) = -0.785399 radians
acos(-0.7071) = 2.356194 radians
atan(0.5773) = 0.523600 radians
atan2(2.0, -2.0) = 2.356194 radians

This C program demonstrates inverse trigonometric functions using the math library. It calculates the arcsine (asin), arccosine (acos), and arctangent (atan) of given values. It also uses atan2() to find the angle between two coordinates, accounting for the correct quadrant. The results are printed in radians, showing how to apply these functions with <math.h> in C.

Common Mistakes and Best Practices

  • Forgetting to Include <math.h>: Always include this header to access trigonometric functions.​
  • Not Linking Math Library: When compiling, link the math library using -lm flag:​
  • gcc program.c -o program -lm
  • Using Degrees Directly: Convert degrees to radians before passing to trigonometric functions.​
  • Handling Undefined Values: Be cautious with angles where tangent is undefined (e.g., 90 degrees or Ï€/2 radians).​

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.