C cos() Function

Definition
In C, cos() returns the cosine of an angle. The angle is in radians. cos(0) is 1.
You pass one double. The result is a double from -1 to 1. Include <math.h>.
Syntax
Pass the angle in radians. Start with this call.
| Call | What it does |
|---|
cos(0.0) | Returns 1. |
Include <math.h>. On GCC, link with -lm. Convert degrees with rad = deg * acos(-1.0) / 180.0.
Library declarations
double cos(double x);
float cosf(float x);
long double cosl(long double x);
Parameters
| Parameter | Description |
|---|
| x | Required. The angle in radians. |
Return Value
| Return Value | Description |
|---|
| Cosine | A double from -1 to 1. |
C Examples
Example 1 — Basic Cosine Values
Use a portable pi from acos(-1.0) and print a few known results.
#include <stdio.h>
#include <math.h>
int main(void) {
double pi = acos(-1.0);
printf("cos(0) = %.6f\n", cos(0.0));
printf("cos(pi/3) = %.6f\n", cos(pi / 3.0));
printf("cos(pi) = %.6f\n", cos(pi));
return 0;
}
cos(0) = 1.000000
cos(pi/3) = 0.500000
cos(pi) = -1.000000
How It Works
- Build pi.
acos(-1.0) is a portable way to get π. - cos(0) is 1. Angle zero points along the positive x-axis.
- cos(π/3) is 0.5. That is the classic 60° cosine.
- cos(π) is -1. A half turn points left on the unit circle.
Example 2 — Convert Degrees First
Most people think in degrees. Convert before calling cos.
#include <stdio.h>
#include <math.h>
int main(void) {
double pi = acos(-1.0);
double deg = 60.0;
double rad = deg * pi / 180.0;
printf("60 degrees = %.6f radians\n", rad);
printf("cos(60°) = %.6f\n", cos(rad));
return 0;
}
60 degrees = 1.047198 radians
cos(60°) = 0.500000
How It Works
- Start with degrees.
deg = 60.0 is human-friendly. - Convert. Multiply by
pi / 180.0 to get radians. - Call cos. Pass only the radian value.
- Result is 0.5. Same as
cos(π/3) from Example 1.
Example 3 — Unit-Circle Pair With sin()
Cosine and sine together give the (x, y) point on the unit circle.
#include <stdio.h>
#include <math.h>
int main(void) {
double pi = acos(-1.0);
double t = pi / 2.0;
printf("x = cos = %.6f\n", cos(t));
printf("y = sin = %.6f\n", sin(t));
return 0;
}
x = cos = 0.000000
y = sin = 1.000000
How It Works
- Angle is π/2. That is 90° on the unit circle.
- cos is ~0. The x-coordinate is at the top of the circle.
- sin is ~1. The y-coordinate reaches its maximum.
- Tiny rounding. You may see a value like
6e-17 instead of exact zero—normal for floating point.
Frequently Asked Questions
cos() returns the cosine of an angle in radians. cos(0) is 1.
x is required. It is the angle in radians.
A double from -1 to 1.
Include <math.h>. On GCC, link with -lm.
No. Convert degrees to radians first. 180 degrees is π radians.
Next: cosh()
Learn hyperbolic cosine—a different function from trigonometric cos.
cosh() tutorial →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful