C Math cos() Function

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
<math.h>

What You’ll Learn

The cos() function computes the cosine of an angle. In C, the angle must be in radians. The result is always between −1 and 1—perfect for waves, rotations, and graphics.

01

Cosine

Trig function.

02

math.h

Link with -lm.

03

Radians in

Not degrees.

04

Range

[−1, 1].

05

Unit circle

x-coordinate.

06

vs cosh

Different fn.

Definition and Usage

Cosine relates an angle to a ratio on a right triangle: adjacent ÷ hypotenuse. On the unit circle (radius 1), cos(θ) is simply the x-coordinate at angle θ.

In C, cos(x) expects x in radians. One full turn is radians (360°). Half a turn is π radians (180°).

💡
Beginner Tip

60° is M_PI / 3.0 radians, and cos(M_PI / 3.0) is 0.5. Do not pass 60 directly—that is 60 radians, not 60 degrees.

📝 Syntax

Standard C declaration:

C
double cos(double x);

Related variants

C
float cosf(float x);           /* <math.h> */
long double cosl(long double x); /* <math.h> */

Parameters

  • x — angle in radians.

Return Value

  • Cosine of x, in the range [−1, 1].
  • cos(0) returns 1.0; cos(M_PI / 2) returns 0.0.
  • cos(M_PI) returns -1.0.

Headers and linking

  • #include <math.h>
  • Compile: gcc cos.c -std=c11 -o cos -lm
  • On MSVC, define _USE_MATH_DEFINES before <math.h> for M_PI.

⚡ Quick Reference

CallAngleResult
cos(0)1.0
cos(M_PI / 6)30°0.866 (√3/2)
cos(M_PI / 3)60°0.5
cos(M_PI / 2)90°0.0
cos(M_PI)180°-1.0
Forward
cos(angle)

Radians in

Degrees
deg * M_PI/180

Convert first

Partner
sin(angle)

y on circle

Not this
cosh(x)

Hyperbolic

Examples Gallery

Compile every example with gcc file.c -std=c11 -o out -lm. All angles below are converted to radians before calling cos().

📚 Getting Started

Compute cosine of a common angle and print the result.

Example 1 — Cosine of 60 Degrees

Convert 60° to radians, then call cos().

C
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>

int main(void) {
    double degrees = 60.0;
    double radians = degrees * M_PI / 180.0;
    double result = cos(radians);

    printf("Angle:   %.1f degrees (%.4f radians)\n", degrees, radians);
    printf("cos() =  %.3f\n", result);

    return 0;
}

How It Works

60° equals π/3 radians. On a unit circle, the x-coordinate at that angle is exactly 0.5.

Example 2 — cos(0) Equals 1

At zero radians, cosine is at its maximum.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    printf("cos(0) = %.1f\n", cos(0.0));

    return 0;
}

How It Works

On the unit circle, angle 0 points to (1, 0). The x-value—cosine—is 1.

📈 Practical Patterns

Degree conversion, circular motion, and comparison with sin.

Example 3 — Reusable Degrees-to-Radians Helper

Wrap the conversion so you never forget radians.

C
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>

static double deg_to_rad(double degrees) {
    return degrees * M_PI / 180.0;
}

int main(void) {
    double angles[] = { 0.0, 30.0, 45.0, 60.0, 90.0 };
    size_t i;

    printf(" deg    cos()\n");
    for (i = 0; i < 5; i++) {
        double rad = deg_to_rad(angles[i]);
        printf("%4.0f  %6.3f\n", angles[i], cos(rad));
    }

    return 0;
}

How It Works

A small helper function keeps degree math out of your trig calls. This table matches standard trigonometry values.

Example 4 — Point on a Unit Circle (Graphics)

Use cos and sin to place a point at a given angle—common in games and UI animation.

C
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>

int main(void) {
    double radius = 100.0;
    double angle_deg = 45.0;
    double angle_rad = angle_deg * M_PI / 180.0;
    double x = radius * cos(angle_rad);
    double y = radius * sin(angle_rad);

    printf("Angle: %.0f degrees\n", angle_deg);
    printf("Point on circle (r=%.0f): (%.2f, %.2f)\n", radius, x, y);

    return 0;
}

How It Works

cos gives the horizontal offset; sin gives the vertical. Together they trace a circle when the angle changes over time.

Example 5 — cos() vs sin() at Key Angles

See how the two basic trig functions complement each other.

C
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>

int main(void) {
    double angles[] = { 0.0, M_PI / 2, M_PI };
    const char *labels[] = { "0", "pi/2", "pi" };
    size_t i;

    printf(" angle   cos()   sin()\n");
    for (i = 0; i < 3; i++) {
        printf(" %5s  %5.1f  %5.1f\n",
               labels[i], cos(angles[i]), sin(angles[i]));
    }

    return 0;
}

How It Works

At 0°, cosine is 1 and sine is 0. At 90°, they swap. At 180°, cosine is −1. Remember: cos = x, sin = y on the unit circle.

🚀 Common Use Cases

  • Graphics and games — rotate sprites, orbit cameras, place objects on circles.
  • Signal processing — model periodic waves (sound, AC voltage).
  • Physics — resolve forces into horizontal components.
  • Robotics — compute joint positions from angles.
  • Pair with sin() — full 2D position from a single angle and radius.

🧠 How cos() Works

1

Receive radians

Angle x in radians (convert from degrees if needed).

Input
2

Evaluate cosine

Library computes cos using optimized polynomial or hardware instructions.

Compute
3

Return in [−1, 1]

Result is a double representing the x-component on a unit circle.

Output
=

Cosine value

Multiply by radius for real-world distances: x = r * cos(θ).

📝 Notes

  • Input is radians, not degrees—convert with degrees * M_PI / 180.0.
  • Output is always in [−1, 1].
  • cos() is an even function: cos(-x) == cos(x).
  • Link with -lm when using GCC/Clang on Unix-like systems.
  • Do not confuse cos() with cosh() (hyperbolic cosine).
  • For very large angles, floating-point precision can affect results—consider reducing the angle modulo if needed.

⚡ Optimization

cos() is implemented in the platform math library and is typically fast enough for most applications. If you call it thousands of times per frame in a game loop, profile first—often the bottleneck is elsewhere. For fixed small angle sets, a lookup table can help, but prefer the library function until profiling proves otherwise.

Conclusion

cos() computes the cosine of an angle in radians and returns a value between −1 and 1. Convert degrees before calling, pair it with sin() for circular motion, and remember it is not the same as cosh().

Continue with cosh() for hyperbolic math, or explore sin() for the vertical component on the unit circle.

💡 Best Practices

✅ Do

  • Include <math.h> and link with -lm
  • Convert degrees to radians before calling cos()
  • Use cosf / cosl when float precision is enough
  • Pair with sin() for 2D circular coordinates
  • Define _USE_MATH_DEFINES on MSVC for M_PI

❌ Don’t

  • Pass degrees directly to cos()
  • Confuse cos() with cosh()
  • Expect output outside [−1, 1]
  • Forget that cos(π/2) may be a tiny non-zero due to floating-point error
  • Reimplement cosine unless profiling demands it

Key Takeaways

Knowledge Unlocked

Five things to remember about cos()

Trigonometric cosine in C, explained simply.

5
Core concepts
📚 02

Range

[−1, 1].

Output
📈 03

Unit circle

x = cos(θ).

Geometry
📄 04

Even fn

cos(−x)=cos(x).

Property
🔄 05

vs cosh

Different.

Compare

❓ Frequently Asked Questions

cos() returns the cosine of an angle given in radians. On a unit circle, cos(theta) is the x-coordinate at angle theta. For example, cos(0) is 1.0 and cos(pi/3) is 0.5 (60 degrees).
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double cos(double x).
Radians. If your angle is in degrees, convert first: radians = degrees * (M_PI / 180.0). Passing 60 directly to cos() is not the same as cos(60 degrees).
The result is always between -1 and 1 inclusive. cos(0) is 1, cos(pi/2) is 0, and cos(pi) is -1.
cos() is a trigonometric cosine (angles in radians, result in [-1, 1]). cosh() is the hyperbolic cosine (uses exponential math, result >= 1). They are different functions despite similar names.
Both take radians. cos(theta) gives the horizontal (x) component on a unit circle; sin(theta) gives the vertical (y) component. cos(pi/2) is 0 while sin(pi/2) is 1.
Did you know?

cos() is an even function: cos(-x) equals cos(x). That symmetry is why a cosine wave looks the same whether time runs forward or backward—unlike sin(), which flips sign for negative angles.

Explore C Math Functions

Continue with cosh() or browse the full math function reference.

Math Functions Index →

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.

5 people found this page helpful