C trunc() Function

Definition
In C, trunc() drops the fraction and moves toward zero. trunc(2.9) is 2, and trunc(-2.9) is -2.
You pass one double. The result is still a double, not an int. Include <math.h>.
Syntax
Pass the number you want to cut toward zero. Start with this call.
| Call | What it does |
|---|
trunc(2.9) | Drops the fraction and returns 2. |
Include <math.h>. On GCC, link with -lm. This is not floor(). Floor goes toward negative infinity.
Library declarations
double trunc(double x);
float truncf(float x);
long double truncl(long double x);
Parameters
| Parameter | Description |
|---|
| x | Required. The double to cut toward zero. |
Return Value
| Return Value | Description |
|---|
| double | The integer value toward zero, stored as a double. |
C Examples
Example 1 — Positive and Negative
Show that both signs move toward zero.
#include <math.h>
#include <stdio.h>
int main(void) {
printf("trunc(3.75) = %.1f\n", trunc(3.75));
printf("trunc(-2.3) = %.1f\n", trunc(-2.3));
printf("trunc(2.5) = %.1f\n", trunc(2.5));
return 0;
}
trunc(3.75) = 3.0
trunc(-2.3) = -2.0
trunc(2.5) = 2.0
How It Works
- Positive.
3.75 loses .75 → 3.0. - Negative.
-2.3 becomes -2.0, closer to zero. - Not round.
2.5 truncates to 2.0, not 3.0. - Link libm. On GCC, remember
-lm.
Example 2 — trunc vs floor
Positives match; negatives diverge.
#include <math.h>
#include <stdio.h>
int main(void) {
double x = -2.3;
printf("trunc(%.1f) = %.1f\n", x, trunc(x));
printf("floor(%.1f) = %.1f\n", x, floor(x));
printf("ceil(%.1f) = %.1f\n", x, ceil(x));
return 0;
}
trunc(-2.3) = -2.0
floor(-2.3) = -3.0
ceil(-2.3) = -2.0
How It Works
- Pick a negative.
-2.3 shows the difference clearly. - trunc. Moves toward zero →
-2.0. - floor. Moves toward −∞ →
-3.0. - ceil. Moves toward +∞ →
-2.0 here.
Example 3 — Split Integer and Fraction
Use trunc for the integer part; subtract to get the fraction (or use modf).
#include <math.h>
#include <stdio.h>
int main(void) {
double x = 5.8;
double whole = trunc(x);
double frac = x - whole;
printf("x = %.1f\n", x);
printf("whole = %.1f\n", whole);
printf("frac = %.1f\n", frac);
printf("as int: %d\n", (int)whole);
return 0;
}
x = 5.8
whole = 5.0
frac = 0.8
as int: 5
How It Works
- Truncate.
trunc(5.8) → 5.0. - Subtract.
5.8 − 5.0 leaves the fractional part. - Cast carefully.
(int)whole is fine when the value fits in int. - Alternative.
modf returns both parts in one call.
Frequently Asked Questions
trunc() drops the fraction and moves toward zero. trunc(2.9) is 2, and trunc(-2.9) is -2.
x is required. It is the double to cut toward zero.
A double holding an integer value. It is not an int.
Include <math.h>. On GCC, link with -lm.
No. trunc() goes toward zero. floor() goes toward negative infinity, so floor(-2.9) is -3.
More Math Functions
Browse the full list of C math.h function tutorials.
Math functions →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