C cos() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

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.

C
cos(0.0);
CallWhat 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
C
double cos(double x);
float cosf(float x);
long double cosl(long double x);

Parameters

ParameterDescription
xRequired. The angle in radians.

Return Value

Return ValueDescription
CosineA 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.

C
#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;
}

How It Works

  1. Build pi. acos(-1.0) is a portable way to get π.
  2. cos(0) is 1. Angle zero points along the positive x-axis.
  3. cos(π/3) is 0.5. That is the classic 60° cosine.
  4. 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.

C
#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;
}

How It Works

  1. Start with degrees. deg = 60.0 is human-friendly.
  2. Convert. Multiply by pi / 180.0 to get radians.
  3. Call cos. Pass only the radian value.
  4. 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.

C
#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;
}

How It Works

  1. Angle is π/2. That is 90° on the unit circle.
  2. cos is ~0. The x-coordinate is at the top of the circle.
  3. sin is ~1. The y-coordinate reaches its maximum.
  4. 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 →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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