trunc() , truncf() , truncl() in C language

Here we will see three functions. These functions are trunc(), truncf() and the truncl(). These functions are used to convert floating point values into truncated form.

The trunc() Function

This function is used to truncate double type value. And return only the integer part. The syntax is like below.

double trunc(double argument)

Example

#include <stdio.h>
#include <math.h>
main() {
double a, b, x, y;
x = 53.26;
y = 75.86;
a = trunc(x);
b = trunc(y);
printf("The value of a: %lf",a);
printf("The value of a: %lf",b);
}

Output

The value of a: 53.000000
The value of a: 75.000000

The truncf() Function

This function is used to truncate floating type value. And return only the integer part. The syntax is like below.

float tuncf(float argument)

Example

#include <stdio.h>
#include <math.h>
main() {
float a, b, x, y;
x = 53.26;
y = 75.86;
a = truncf(x);
b = truncf(y);
printf("The value of a: %f",a);
printf("The value of a: %f",b);
}

Output

The value of a: 53.000000
The value of a: 75.000000

The truncl() Function

This is like trunc() or truncf(). But the main difference is, this function is used to truncate long double type value. And return only the integer part.

The syntax is like below.

long double truncl(long double argument)

Example

#include <stdio.h>
#include <math.h>
main() {
long double a, b, x, y;
x = 53547.55555555555;
y = 78547.55555555523;
a = truncl(x);
b = truncl(y);
printf("The value of a: %Lf",a);
printf("The value of a: %Lf",b);
}

Output

The value of a: 53547.000000
The value of a: 78547.000000

Updated on: 30-Jul-2019

1K+ Views