trunc() Function

The trunc() function in C removes the fractional part of a floating-point number by rounding it toward zero, yielding the nearest integer value that does not exceed the original value in magnitude.


Syntax of trunc()

</>
Copy
double trunc(double x);
float truncf(float x);
long double truncl(long double x);

Parameters

ParameterDescription
xThe value to truncate.

It is important to note that the function simply discards the fractional part and does not perform any rounding. The function returns a floating-point number representing the truncated integral part of the original value.

Return Value

The function returns the nearest integral value (as a floating-point number) that is not larger in magnitude than the original value.


Examples for trunc()

Example 1: Truncating a Positive Floating-Point Number

This example demonstrates how trunc() truncates a positive floating-point number by removing its fractional part.

Program

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

int main() {
    double num = 5.89;
    double result = trunc(num);

    printf("Truncated value: %f\n", result);
    return 0;
}

Explanation:

  1. The program includes the necessary headers <stdio.h> and <math.h>.
  2. A positive floating-point number 5.89 is defined.
  3. The trunc() function is called to remove the fractional part.
  4. The truncated value, which is 5.000000, is printed.

Program Output:

Truncated value: 5.000000

Example 2: Truncating a Negative Floating-Point Number

This example shows how trunc() behaves with a negative floating-point number by discarding its fractional component.

Program

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

int main() {
    double num = -3.75;
    double result = trunc(num);

    printf("Truncated value: %f\n", result);
    return 0;
}

Explanation:

  1. The program starts by including <stdio.h> and <math.h>.
  2. A negative floating-point number -3.75 is defined.
  3. The trunc() function truncates the fractional part, yielding -3.000000.
  4. The resulting value is printed.

Program Output:

Truncated value: -3.000000

Example 3: Using truncf() with a Float Value

This example demonstrates the use of the truncf() function for a float value, achieving similar truncation as with trunc().

Program

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

int main() {
    float num = 7.123f;
    float result = truncf(num);

    printf("Truncated float value: %f\n", result);
    return 0;
}

Explanation:

  1. The program includes the headers <stdio.h> and <math.h>.
  2. A float value 7.123f is declared.
  3. The truncf() function is used to remove the fractional part.
  4. The truncated result, 7.000000, is printed.

Program Output:

Truncated value: 7.000000

Example 4: Truncating a Large Floating-Point Value

This example illustrates the use of trunc() on a larger floating-point value to demonstrate its behavior with significant digits.

Program

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

int main() {
    double num = 12345.6789;
    double result = trunc(num);

    printf("Truncated value: %f\n", result);
    return 0;
}

Explanation:

  1. The required headers <stdio.h> and <math.h> are included.
  2. A large floating-point number 12345.6789 is defined.
  3. The trunc() function truncates the fractional part, yielding 12345.000000.
  4. The result is printed to the console.

Program Output:

Truncated value: 12345.000000