# C programming trunc,truncf and truncl math.h

In C programming the <math.h> trunc,truncf and truncl function round the argument to integer but not larger in magnitude than the argument.The declaration of the functions are given below.

1 float trunc(float x);
2double trunc(double x);
3 long double trunc(long double x);
4 float truncf(float x);
5blong double truncl(long double x);

Parameters:
x -The argument to be rounded off.

Return type
floating point -The rounded value.

All the functions are the same,they round the argument to integer but not larger in magnitude than the argument.The 1st,2nd and the 3rd function have the same name i.e.’trunc’.The 4th and 5th function have different names.What does having a different names mean is explained in detail below.

Some points to note:

i) The trunc function always round toward 0.

ii) If ±∞ is passed ±∞ is returned.

iii) If ±0 is passed ±0 is returned.

iv) If NAN is passed ‘nan’ is returned.

Code example

double d=89.56 , x1=4.35e20 ;

float f=89.56 ;

long double ld=89.56 ;

printf(“%lf”, trunc( d ) ) ; //calls the 2nd version
printf(“\n%f”, trunc( f ) ) ; //calls the 1st version
printf(“\n%Lf”, trunc( ld ) ) ;//calls the 3rd version

printf(“\n%lf”, trunc( x1 ) ) ;
printf(“\n%lf”, trunc( INFINITY ) ) ;

Output in Visual Studio ,

89.000000
89.000000
89.000000
435000000000000000000.000000
inf

Note here the although the fractional value of the arguments passed to the 1st,2nd and 3rd functions are more than 0.5 the truncated value returned is rounded to the integer value just less than the argument.And as stated earlier ‘trunc’ always round toward 0 holds true here.

#### truncf

The ‘truncf‘ function is same as the 1st function.This means the parameter and return type of the function is float type.The ‘f‘ character appended to ‘truncf‘ stands for float and it signify the parameter type and return type of the function.

Code example

float x=900.23 ;

printf(“%f”, trunc( x ) ); //Calls the 1st version
printf(“\n%f”, truncf( 900.23) ); Calls the 4th version

Output in Visual Studio,

900.000000
900.000000

#### truncl

The ‘truncl‘ function is same as the 1st function.This means the parameter and return type of the function is long double type.The ‘l‘ character appended to ‘truncl‘ stands for long double and it signify the parameter type and return type of the function.

Code example

long double x=900.23 ;

printf(“%Lf”, trunc( x ) ); //Calls the 3rd version
printf(“\n%Lf”, truncl( 900.23 ) ); Calls the 5th version

Output,

900.000000
900.000000