trunc in c

What is trunc in c programming?

trunc in c truncates double type value and returns integer value.

This function is defined in math.h header file.

Prototype

Prototype of trunc in c is as follows

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

Parameter

trunc function takes double type value as input parameter.

Return value

trunc function returns truncated value of a given double type value.


Example program for trunc in c

#include<stdio.h>
#include<math.h>
int main()
{
 printf("\n\n\tThe truncated value of 43.5 is %f \n\n",trunc(43.5));
 return 0;
}
Example for trunc in c

While using gcc use below command to add math header.

gcc file_name.c -lm

Code explanation

The trunc function truncates the value which is passed to it. The result is being printed on to the console.

trunc() Man page


Difference between trunc and round in c

trunc in c truncates double type value and returns an integer value. The round function is used to find out the nearest float/double/long double type value of a given number.

Prototypes are as follows

double trunc(double m);

double round(double x);
trunc in cround in c
Truncates double type value and returns integer value.Used to find out nearest float/double/long double type value of given number.
double trunc(double x);double round(double x);

Difference between trunc and abs in c

trunc in c truncates double type value and returns an integer value. abs in c is used to find out the absolute value of a given number.

trunc is included in math.h header file. abs is included in stdlib.h header file.

trunc function works on double type value. abs function works on integer value.

Prototypes are as follows

double trunc(double x);

int abs(int m);
trunc( )abs( )
Truncates double type value and returns integer value.Used to find out absolute value of given number.
double trunc(double x);int abs(int m);
Included in math.h header file.Included in stdlib.h header file.

Difference between trunc and floor in c

trunc in c truncates double type value and returns an integer value. the floor in c is used to find out the nearest integer which is less than or equal to the given value.

Prototypes are as follows

double trunc(double x);

double floor(double y);
trunc( )floor( )
Truncates double type value and returns integer value.Used to find out nearest integer which is less than or equal to given value.
double trunc(double x);double floor(double y);

Difference between trunc and ceil in c

trunc in c truncates double type value and returns an integer value. ceil in c is used to find out the nearest integer value which is greater than or equal to the given number.

Prototypes are as follows

double trunc(double x);

double ceil(double y);
trunc( )ceil( )
Truncates double type value and returns integer value.Used to find out nearest integer value which is greater than or equal to given number.
double trunc(double x);double ceil(double y);

People also ask for

What is the difference between trunc and truncf in c?

trunc in c truncates double type value and returns an integer value. truncf in c is used to truncate floating type value.

What is the difference between trunc and truncl in c?

trunc in c truncates double type value and returns an integer value. truncl in c is used to truncate long double type value.