Ad space (banner)
🔧C Lessons
Lesson 45 / 68

Rounding Numbers (floor, ceil, round)

This lesson covers rounding decimals in C — floor, ceil, and round — so you can pick the right rounding method for the job. It's for anyone searching "C floor ceil round usage."

floor() rounds down, ceil() rounds up, and round() rounds to the nearest whole number. It helps to picture "erasing down to the smaller number," "bumping up," and "rounding to whichever is closer." They're provided by the math.h header.

The example shows floor(3.7) giving 3.0, ceil(3.2) giving 4.0, and round(3.5) giving 4.0 — similar decimal values, but each function produces a different result. Notice they all return a double.

A common early mistake is not knowing that using these functions requires including math.h, and that some environments need the -lm flag at compile time. C sometimes requires an explicit linking step to use math functions.

In real projects, which rounding method you choose changes the result — rounding a monetary calculation's fraction, or aligning decimal places for display — so choose carefully depending on the situation.

📖 Reference code
✍️ Your code
Type your code, then press "Run"

🧪 This site can't compile or run C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser — nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)