Related Articles

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

• Last Updated : 04 Apr, 2018

All three functions are used to remove digits after decimal point and return the modified decimal number.

trunc() : Truncates a double value after the decimal point and gives the integer part as the result. The return value and the arguments are of the type double.

Syntax :
double trunc(double x);

Parameters:
x :It takes a double value as an input and then truncates the values after the decimal point.

Return Value :
It returns a double value whose values after the decimal point is 0 only.

Examples:

```Input : 3.5
Output : 3.0

Input : -3.8
Output : -3.0
```
 `// C program to demonstrate ``// trunc() function``#include `` ` `// library containing trunc function``#include      `` ` `// Driver function``int` `main()``{``    ``// using trunc function which return``    ``// Truncated value of the input ``    ``double` `x1 = 2.0, x2 = 3.9, x3 = -3.3, x4 = 4.9;``    ``printf``(``" Truncated value is %lf \n"``, trunc(x1) );``    ``printf``(``" Truncated value is %lf \n"``, trunc(x2) );`` ` `    ``// For negative values``    ``printf``(``" Truncated value is %lf \n"``, trunc(x3) );``    ``printf``(``" Truncated value is %lf \n"``, trunc(x4) ); ``    ``return` `0;``}`
Output:
```Truncated value is 2.000000
Truncated value is 3.000000
Truncated value is -3.000000
Truncated value is 4.000000
```

truncf() : It works same as trunc with the difference that it is for float instead of double. Takes a float value, removes digits after decimal point and return modified float.

Syntax :
float truncf(float x);

Parameters:
x :It takes a floating point value as an input and then truncates the values after the decimal point.

Return Value :
It returns a float whose values after the decimal point is 0 only.

Examples:

```Input : 4.5
Output : 4.0

Input : -2.8
Output : -2.0
```
 `// C program to demonstrate truncf() function``#include ``#include      `` ` `// Driver function``int` `main()``{``    ``float` `x1 = 2.0, x2 = 3.9, x3 = -3.3, x4 = 4.9;`` ` `    ``// using truncf function which return``    ``// Truncated value of the input ``    ``printf``(``" Truncated value is %f \n"``, truncf(x1) );``    ``printf``(``" Truncated value is %f \n"``, truncf(x2) );`` ` `    ``// For negative values``    ``printf``(``" Truncated value is %f \n"``, truncf(x3) );``    ``printf``(``" Truncated value is %f \n"``, truncf(x4) ); ``    ``return` `0;``}`
Output:

```Truncated value is 2.000000
Truncated value is 3.000000
Truncated value is -3.000000
Truncated value is 4.000000
```

truncl() : This works similar for long double and functionality wise same as trunc() and truncf()

Syntax :
long double truncl (long double x);

Parameters:
x :It takes a long double value as an input and then truncates the values after the decimal point.

Return Value :
It returns a decimal value whose values after the decimal point is 0 only.

Examples:

```Input : 4351.5
Output : 4351.0

Input : -2008.8
Output : -2008.0
```

In case of positive values:

 `// C program to demonstrate truncl() function``#include ``#include      `` ` `// Driver function``int` `main()``{``    ``long` `double` `x1 = 2.0, x2 = 3.9, x3 = -3.3, x4 = 4.9;`` ` `    ``// using truncf function which return``    ``// Truncated value of the input ``    ``printf``(``" Truncated value is %Lf \n"``, truncl(x1) );``    ``printf``(``" Truncated value is %Lf \n"``, truncl(x2) );`` ` `    ``// For negative values``    ``printf``(``" Truncated value is %Lf \n"``, truncl(x3) );``    ``printf``(``" Truncated value is %Lf \n"``, truncl(x4) ); ``    ``return` `0;``}`
Output:
```Truncated value is 2.000000
Truncated value is 3.000000
Truncated value is -3.000000
Truncated value is 4.000000
```

We can use trunc() function whenever we need to calculate the integer part from a double data type. The advantage of this function is that whatever the decimal value may be the integer part remains same. In the ceil or floor or round functions the integer value changes but in trunc function it does not.

Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.

My Personal Notes arrow_drop_up