C trunc() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

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.

C
trunc(2.9);
CallWhat 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
C
double trunc(double x);
float truncf(float x);
long double truncl(long double x);

Parameters

ParameterDescription
xRequired. The double to cut toward zero.

Return Value

Return ValueDescription
doubleThe integer value toward zero, stored as a double.

C Examples

Example 1 — Positive and Negative

Show that both signs move toward zero.

C
#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;
}

How It Works

  1. Positive. 3.75 loses .75 → 3.0.
  2. Negative. -2.3 becomes -2.0, closer to zero.
  3. Not round. 2.5 truncates to 2.0, not 3.0.
  4. Link libm. On GCC, remember -lm.

Example 2 — trunc vs floor

Positives match; negatives diverge.

C
#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;
}

How It Works

  1. Pick a negative. -2.3 shows the difference clearly.
  2. trunc. Moves toward zero → -2.0.
  3. floor. Moves toward −∞ → -3.0.
  4. 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).

C
#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;
}

How It Works

  1. Truncate. trunc(5.8) → 5.0.
  2. Subtract. 5.8 − 5.0 leaves the fractional part.
  3. Cast carefully. (int)whole is fine when the value fits in int.
  4. 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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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