Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

trunc — truncation toward zero

The trunc function from <math.h> removes the fractional part of a double by rounding toward zero. Its result is still a double, not an integer type.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <math.h>
double trunc(double x);

trunc(x), declared in <math.h> as double trunc(double x);, returns the nearest integer value whose magnitude does not exceed the argument's magnitude. For finite arguments, this means removing the fractional part toward zero.

For example, trunc(3.75) produces 3.0, while trunc(-3.75) produces -3.0, not -4.0. The result remains a double: the function does not convert it to int. An argument that already has an integer value retains its value.

Examples

#include <math.h>
#include <stdio.h>

int main(void)
{
    printf("%.1f\n", trunc(3.75));
    printf("%.1f\n", trunc(-3.75));
    return 0;
}

Result

3.0
-3.0

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources