C acos() Function

Definition
In C, acos() returns the angle whose cosine is x. The angle is in radians, from 0 to π. acos(1) is 0, and acos(0) is π/2.
You pass one double. It must be from -1 to 1. The result is a double in radians, not degrees. Include <math.h>.
Syntax
Pass a cosine from -1 to 1. Start with this call.
| Call | What it does |
|---|
acos(0.0) | Returns π/2, the angle whose cosine is 0. |
Include <math.h>. On GCC, link with -lm. The result is radians.
Library declarations
double acos(double x);
float acosf(float x);
long double acosl(long double x);
Parameters
| Parameter | Description |
|---|
| x | Required. A cosine from -1 to 1. |
Return Value
| Return Value | Description |
|---|
| Radians | The angle from 0 to π whose cosine is x. |
| NaN | Returned when x is outside -1 to 1. |
C Examples
Example 1 — Common acos Values
Print radians for a few cosine inputs in the valid domain.
#include <math.h>
#include <stdio.h>
int main(void) {
printf("acos(1) = %.4f\n", acos(1.0));
printf("acos(0) = %.4f\n", acos(0.0));
printf("acos(0.5) = %.4f\n", acos(0.5));
printf("acos(-1) = %.4f\n", acos(-1.0));
return 0;
}
acos(1) = 0.0000
acos(0) = 1.5708
acos(0.5) = 1.0472
acos(-1) = 3.1416
How It Works
- Include math.h. Declares
acos; link with -lm on GCC. - Endpoints.
acos(1) is 0; acos(-1) is π. - Middle values.
acos(0) is π/2; acos(0.5) is π/3. - Units are radians. These numbers are not degrees until you convert.
Example 2 — Convert Radians to Degrees
Multiply by 180 / π when you want a degree reading.
#include <math.h>
#include <stdio.h>
int main(void) {
double x = 0.5;
double rad = acos(x);
double pi = acos(-1.0);
double deg = rad * 180.0 / pi;
printf("acos(%.1f) = %.4f rad\n", x, rad);
printf(" = %.2f deg\n", deg);
return 0;
}
acos(0.5) = 1.0472 rad
= 60.00 degHow It Works
- Get radians first.
acos(0.5) ≈ 1.0472. - Get π portably.
acos(-1.0) equals π without needing M_PI. - Scale to degrees. Multiply by
180.0 / pi → 60. - Keep rad for math. Pass radians into
cos/sin; use degrees only for display.
Example 3 — Portable π with acos(-1)
A common ISO-friendly way to define π when M_PI is missing.
#include <math.h>
#include <stdio.h>
int main(void) {
const double PI = acos(-1.0);
printf("PI ≈ %.10f\n", PI);
printf("cos(PI/3) ≈ %.4f\n", cos(PI / 3.0));
return 0;
}
PI ≈ 3.1415926536
cos(PI/3) ≈ 0.5000
How It Works
- Ask for acos(-1). That angle is exactly π radians.
- Store as a constant. Reuse
PI for conversions and trig. - Check with cos.
cos(π/3) should be about 0.5. - Inverse pair.
cos and acos undo each other on the valid domain.
Frequently Asked Questions
acos() returns the angle, in radians, whose cosine is x. acos(1) is 0 and acos(0) is π/2.
x is required. It is a double from -1 to 1.
A double in radians, from 0 to π. If x is outside -1 to 1, the result is NaN.
Include <math.h>. On GCC, link with -lm.
No. The result is radians. Multiply by 180/π when you want degrees.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
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