w3resource

C cos() function

C cos() function - arc cosine functions

Syntax:

double cos(double x)

The cos() function is used to calculate the cosine of x. The value x is expressed in radians. If x is too large, a partial loss of significance in the result might occur.

Parameters:

Name Description Required /Optional
x Represent an angle expressed in radians. Required

Return value from cos()

  • The function returns the cosine of x.

Example: cos() function

The following example shows the usage of cos() function.


#include <math.h>
#include <stdio.h>
 
int main(void)
{
   double x, y;
 
   x = 0;
   y = cos(x);
   printf("cos( %lf ) = %lf\n", x, y);
   x = 1;
   y = cos(x);
   printf("\ncos( %lf ) = %lf\n", x, y);
   x = 45;
   y = cos(x);
   printf("\ncos( %lf ) = %lf\n", x, y);
}

Output:

cos( 0.000000 ) = 1.000000

cos( 1.000000 ) = 0.540302

cos( 45.000000 ) = 0.525322 

C Programming Code Editor:

Previous C Programming: C atan2()
Next C Programming: C cosh()



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

What's the point of const pointers?

const is a tool which you should use in pursuit of a very important C++ concept:

Find bugs at compile-time, rather than run-time, by getting the compiler to enforce what you mean.

Even though it does not change the functionality, adding const generates a compiler error when you're doing things you didn't mean to do. Imagine the following typo:

void foo(int* ptr)
{
    ptr = 0;// oops, I meant *ptr = 0
}

If you use int* const, this would generate a compiler error because you're changing the value to ptr. Adding restrictions via syntax is a good thing in general. Just don't take it too far -- the example you gave is a case where most people don't bother using const.

Ref : https://bit.ly/33Cdn3Q