The trunc() function removes the fractional part of a floating-point number by rounding toward zero. It keeps the integer portion without rounding up or down to the nearest whole number.
01
Truncate
Drop frac.
02
math.h
Link -lm.
03
Toward 0
Not nearest.
04
Negatives
-2.3 → -2.
05
vs floor
On negatives.
06
double out
Cast to int.
Fundamentals
Definition and Usage
Truncation means discarding everything after the decimal point by moving toward zero on the number line. trunc(3.75) becomes 3.0; trunc(-2.3) becomes -2.0—not −3.
This is different from round(), which picks the nearest integer, and from floor(), which moves toward negative infinity. Use trunc when you literally want to chop off decimals without changing the sign’s magnitude beyond that.
💡
Beginner Tip
Need both whole and fractional parts? modf(x, &whole) splits a number in one call. trunc gives only the integral part toward zero—use it for display, grid snapping, or integer-only logic.
If x is already an integer, NaN, or infinity, the result equals x.
Headers and linking
#include <math.h>
Compile: gcc trunc.c -std=c11 -o trunc -lm
Cheat Sheet
⚡ Quick Reference
Call
Meaning
Result
trunc(3.75)
Positive fraction
3.0
trunc(-2.3)
Toward zero
-2.0
trunc(5.0)
Already integer
5.0
trunc(2.5)
Not rounded up
2.0
floor(-2.3)
vs trunc (neg.)
-3.0
Truncate
trunc(x)
Toward zero
Floor
floor(x)
Toward −∞
Round
round(x)
Nearest int
Split
modf(x, &w)
Whole + frac
Hands-On
Examples Gallery
Compile every example with gcc file.c -std=c11 -o out -lm. Truncation is common in display formatting, game coordinates, and splitting dollars from cents.
trunc(19.99) is 19.0, not 20—truncation never rounds up. modf returns the fractional 0.99 and stores the integral part in whole. For currency, choose trunc vs round based on your business rules.
Applications
🚀 Common Use Cases
Display formatting — show integer part of a measurement without rounding up.
Game coordinates — snap to tile index by truncating pixel position.
Signal processing — extract integer cycles from a phase value.
Type conversion — safer than raw (int)x when you need explicit truncation semantics.
Pair with modf() — when you need both whole and fractional parts.
🧠 How trunc() Works
1
Receive double x
Any finite floating-point input.
Input
2
Drop fractional part
Move toward zero on the number line—no rounding to nearest.
Compute
3
Return as double
Integral value stored in floating-point format.
Output
=
🔢
Truncated value
Cast to int when you need a whole number for indexing.
Important
📝 Notes
Rounds toward zero—not toward negative infinity like floor.
Returns double, not int—cast explicitly when needed.
trunc(-2.3) is −2; do not assume “floor behavior” on negatives.
Does not round to nearest—trunc(2.9) is 2, not 3.
NaN and infinity pass through unchanged.
Link with -lm when using GCC/Clang on Unix-like systems.
Performance
⚡ Optimization
trunc() is implemented in the platform math library. For positive values in a known-safe range, some code uses (int)x as a fast path—but that only matches trunc semantics for values that fit in int and when truncation toward zero is intended. Prefer trunc for clarity and correct negative handling.
Wrap Up
Conclusion
trunc() removes the fractional part of a floating-point number by rounding toward zero. It matches intuitive “chop decimals” behavior for positives and differs from floor() on negatives.
You have now covered all core C math functions in this series. Browse the math function index or revisit floor() and round() to compare rounding strategies.
Use trunc when you want to drop decimals toward zero
Compare with floor, ceil, and round for clarity
Use modf when you need the fractional part too
Cast to int after checking range
❌ Don’t
Assume trunc equals floor on negatives
Use trunc when nearest-integer rounding is needed
Forget the return type is double
Cast huge doubles to int without overflow checks
Confuse truncation with banker's rounding
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about trunc()
Truncate toward zero in C, explained simply.
5
Core concepts
🔢01
Toward 0
Drop frac.
Basics
📚02
3.75→3
Positive.
Example
📈03
-2.3→-2
Not -3.
Negatives
📄04
vs round
Not nearest.
Compare
🔄05
modf
Split parts.
Pair
❓ Frequently Asked Questions
trunc(x) removes the fractional part of x by rounding toward zero. For example, trunc(3.75) is 3.0 and trunc(-2.3) is -2.0. It does not round to the nearest integer.
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double trunc(double x).
For positive numbers they match. For negatives, trunc(-2.3) is -2 (toward zero) while floor(-2.3) is -3 (toward negative infinity). Pick trunc when you want to drop decimals without moving further from zero.
round(x) goes to the nearest integer (0.5 ties away from zero). trunc(x) always chops the fractional part toward zero. round(2.5) is 3.0; trunc(2.5) is 2.0.
No. trunc() returns double (e.g. 3.0). Cast when you need int: int n = (int)trunc(x); ensure the value fits in int range before casting.
truncf(x) takes float and returns float. truncl(x) takes long double and returns long double. Use the variant matching your variable type.
Did you know?
In C, casting a double to int with (int)x also truncates toward zero—but only when the value fits in int range. For clarity and large magnitudes, prefer trunc() or modf() from <math.h>.