A cube is a three-dimensional shape with six equal square faces. Given the side length s, the surface area is 6s² (six faces, each with area s²) and the volume is s³. This program reads the side length and computes both values using only basic arithmetic — no need for pow() and the -lm flag when the exponent is a small integer.
The original post used void main(), pow(side, 3) (requiring -lm), and broken \n. This rewrite uses side * side * side instead of pow(), which avoids the math library dependency and is more efficient for integer exponents.
Cube Formulas
| Property | Formula | Side = 5 |
|---|---|---|
| Surface area | 6 × s² | 6 × 25 = 150 |
| Volume | s³ | 5 × 5 × 5 = 125 |
| Diagonal (face) | s√2 | 5 × 1.4142 ≈ 7.071 |
| Diagonal (space) | s√3 | 5 × 1.7321 ≈ 8.660 |
C Program: Surface Area and Volume of a Cube
/* Surface area and volume of a cube
* Surface area = 6 * s^2
* Volume = s^3
* Compile: gcc -ansi -Wall -Wextra cube.c -o cube */
#include <stdio.h>
int main(void)
{
double side, surface_area, volume;
printf("Enter the length of a side: ");
if (scanf("%lf", &side) != 1 || side <= 0.0) {
printf("Error: side must be a positive number.\n");
return 1;
}
surface_area = 6.0 * side * side;
volume = side * side * side;
printf("Side = %.4f\n", side);
printf("Surface area = %.4f (6 * %.4f^2)\n", surface_area, side);
printf("Volume = %.4f (%.4f^3)\n", volume, side);
return 0;
}
How to Compile and Run
gcc -ansi -Wall -Wextra cube.c -o cube
./cube
Sample Output
Enter the length of a side: 5 Side = 5.0000 Surface area = 150.0000 (6 * 5.0000^2) Volume = 125.0000 (5.0000^3) Enter the length of a side: 2.5 Side = 2.5000 Surface area = 37.5000 (6 * 2.5000^2) Volume = 15.6250 (2.5000^3)
Code Explanation
side * side * sideinstead ofpow(side, 3)—pow()is a general-purpose floating-point power function from<math.h>that requires linking with-lm. For small integer exponents like 2 and 3, direct multiplication is faster, requires no library, and compiles without-lm. Reservepow()for non-integer or large exponents (e.g.,pow(x, 1.5)).doubleinstead offloat—doubleprovides about 15–16 significant decimal digits of precision.floatprovides only 6–7. For geometric calculations where inputs might be large (e.g., a side of 1000 meters), float’s limited precision causes visible rounding errors.doubleis the default floating-point type in C and is almost always preferable.scanf("%lf", &side)notscanf("%f")— when reading into adoublewithscanf, you must use%lf(long float). Using%fwith a double pointer reads only 4 bytes (float-sized) and leaves the remaining 4 bytes of the double uninitialized — a silent bug.printfuses%ffor both float and double (it promotes float to double), butscanfdoes not.- Validation:
side <= 0.0— a cube with side zero or negative is geometrically meaningless. Checking this before computing prevents outputting negative or zero areas and volumes that would confuse the user.
What This Program Teaches
- When not to use pow() — for s², s³, s⁴: use multiplication.
pow(x, 2)may be slower thanx * xbecause pow() handles fractional exponents and special cases (NaN, infinity). Some compilers optimizepow(x, 2.0)tox * xautomatically, but it is clearer to write the multiplication directly. - %f vs %lf in scanf — this scanf vs printf asymmetry trips up many beginners. Always use
%lfin scanf for double,%ffor float. In printf,%fworks for both (default argument promotion converts float to double). The asymmetry is a consequence of how variadic functions work in C. - Surface area and volume scale differently — doubling the side doubles each edge, but surface area scales by 4× (2² = 4) and volume scales by 8× (2³ = 8). This is the square-cube law: relevant in physics, biology (why ants can lift many times their body weight but elephants cannot) and engineering.
Related Programs
- Area of Isosceles Triangle in C
- Simple Interest in C
- sizeof Data Types in C
- Execution Time with clock() in C
- All Data Types in C
Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
|
C Programming: A Modern Approach — K.N. King (India) |
(US)
Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.