C cos()

The cos() function computes the cosine of an argument.

C cos() Prototype

double cos(double x);

Function cos() takes a single argument in radians and returns a value in type double.

The value returned by cos() is always in the range: -1 to 1.

It is defined in <math.h> header file.


[Mathematics] cosx = cos(x) [In C Programming]

In order to use cos() for floats or long double, you can use the following prototype:

long double cosl(long double x);
float cosf(float x);

C cos() range

The arguments passed to cos() function can be any value, either negative or positive.

Example: C cos() function

#include <stdio.h>
#include <math.h>
#define PI 3.141592654

int main()
{
   double arg = 30, result;

   // Converting to radian
   arg = (arg * PI) / 180;
   result = cos(arg);

   printf("cos of %.2lf radian = %.2lf", arg, result);

   return 0;
}

Output

cos of 0.52 radian = 0.87