The cos function in C programming is a Math Library Function, used to calculate the trigonometric cosine value for the specified expression. The syntax of the cos is as shown below.
double cos(double number);
The cos function will return the value between -1 and 1. Before we get into to the syntax, let us see the mathematical formula behind this Trigonometry Cosine function:
cos(x) = Length of the Adjacent Side / Length of the Hypotenuse
cos function in C Example
The cos function in the math library allows you to find the trigonometry Cosine for the specified values.
This C program, ask the user to enter his/her own value, and then it will find the cosine value of the user-specified one
TIP: Please refer acos function and the math functions article in C Programming to calculate the Arc Cosine of the specified expression.
#include <stdio.h>
#include <math.h>
int main()
{
double cosValue, number;
printf(" Please Enter the Value to calculate Cosine : ");
scanf("%lf", &number);
cosValue = cos(number);
printf("\n Cosine value of %lf = %f ", number, cosValue);
return 0;
}

How to convert degrees to radians to use in the C cos function?
As we mentioned earlier, the C cos() function accepts radians as input and returns the cosine value. However, if we want to use degrees, we must convert degrees to radians and then apply the cos() method.
In this cosine function example, we are allowing the user to enter the value in degrees. Then we are converting the degrees to Radians. And finally, we are finding the cosine value of the radian.
#include <stdio.h>
#include <math.h>
#define PI 3.14
int main()
{
double cosValue, radianVal, degreeVal;
printf(" Please Enter an Angle in degrees : ");
scanf("%lf", °reeVal);
// Convert Dgree Value to Radian
radianVal = degreeVal * (PI/180);
cosValue = cos(radianVal);
printf("\n Cosine value of %f = %f ", degreeVal, cosValue);
return 0;
}
Please Enter an Angle in degrees : 35
Cosine value of 35.000000 = 0.819330
TIP: Please refer to the sin and tan functions to find the sine and tangent values.
C cosf and cosl functions
Apart from the default cos() function, there are two other variants, such as cosf() and cosl() methods, to find the cosine value of the given value.
- If the data type is double, use the cos() function.
- If the data type is a float and we need the result to be a float, use the cosf() function. It avoids converting a float to double and then convert result (double) back to a float.
- Similarly, if the data type is long double, use the cosl() function instead of the cos() method.
In the following program, we use the cosf() and cosl() functions.
#include <stdio.h>
#include <math.h>
int main()
{
double a = 1.0;
double res1 = cos(a);
printf("%f\n", res1);
float b = 0.75f;
float res2 = cosf(b);
printf("%f\n", res2);
long double c = -1.0L;
long double res3 = cosl(c);
printf("%.10Lf\n", res3);
}
0.540302
0.731689
0.5403023058