C cbrt() Function

Definition
In C, cbrt() returns the cube root of x. cbrt(8) is 2. A negative input is allowed and gives a negative result, so cbrt(-8) is -2.
You pass one double. The result is a double with the same sign as x. Include <math.h>.
Syntax
Pass any real number. Start with this call.
| Call | What it does |
|---|
cbrt(8.0) | Returns 2, because 2 × 2 × 2 is 8. |
Include <math.h>. On GCC, link with -lm. Unlike sqrt(), a negative input is valid.
Library declarations
double cbrt(double x);
float cbrtf(float x);
long double cbrtl(long double x);
Parameters
| Parameter | Description |
|---|
| x | Required. Any real double, including negatives. |
Return Value
| Return Value | Description |
|---|
| Cube root | A double with the same sign as x. |
C Examples
Example 1 — Common Cube Roots
Print cube roots for a few perfect cubes and zero.
#include <math.h>
#include <stdio.h>
int main(void) {
printf("cbrt(0) = %.1f\n", cbrt(0.0));
printf("cbrt(8) = %.1f\n", cbrt(8.0));
printf("cbrt(27) = %.1f\n", cbrt(27.0));
return 0;
}
cbrt(0) = 0.0
cbrt(8) = 2.0
cbrt(27) = 3.0
How It Works
- Include math.h. Declares
cbrt; link with -lm on GCC. - Zero stays zero.
cbrt(0) is 0. - Perfect cubes.
2³ = 8 and 3³ = 27. - Double results. Even exact roots come back as
double.
Example 2 — Negative Numbers
Cube roots of negatives are negative. Prefer cbrt over pow(x, 1.0/3.0) here.
#include <math.h>
#include <stdio.h>
int main(void) {
double x = -8.0;
double r = cbrt(x);
printf("cbrt(%.1f) = %.1f\n", x, r);
printf("check: %.1f^3 = %.1f\n", r, r * r * r);
return 0;
}
cbrt(-8.0) = -2.0
check: -2.0^3 = -8.0
How It Works
- Pass a negative.
cbrt stays in the reals. - Sign matches. The root is
-2. - Verify by cubing.
(-2)³ = -8. - Avoid pow for this.
pow(-8, 1.0/3.0) is often NaN on real-valued libm.
Example 3 — Edge Length from Volume
For a cube, edge length is the cube root of volume.
#include <math.h>
#include <stdio.h>
int main(void) {
double volume = 64.0;
double edge = cbrt(volume);
printf("volume = %.1f\n", volume);
printf("edge = %.1f\n", edge);
return 0;
}
How It Works
- Start with volume. A cube of volume
64. - Take the cube root. That is the edge length.
- Check mentally.
4 × 4 × 4 = 64. - Same idea as sqrt for squares. Use
sqrt for area → side; cbrt for volume → edge.
Frequently Asked Questions
cbrt() returns the cube root of x. cbrt(8) is 2, and cbrt(-8) is -2.
x is required. Any real double is allowed, including negatives.
A double with the same sign as x.
Include <math.h>. On GCC, link with -lm.
Yes. A negative input returns a negative cube root.
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