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()
double trunc(double x);
float truncf(float x);
long double truncl(long double x);
Parameters
Parameter | Description |
---|---|
x | The 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
#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:
- The program includes the necessary headers
<stdio.h>
and<math.h>
. - A positive floating-point number
5.89
is defined. - The
trunc()
function is called to remove the fractional part. - 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
#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:
- The program starts by including
<stdio.h>
and<math.h>
. - A negative floating-point number
-3.75
is defined. - The
trunc()
function truncates the fractional part, yielding-3.000000
. - 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
#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:
- The program includes the headers
<stdio.h>
and<math.h>
. - A float value
7.123f
is declared. - The
truncf()
function is used to remove the fractional part. - 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
#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:
- The required headers
<stdio.h>
and<math.h>
are included. - A large floating-point number
12345.6789
is defined. - The
trunc()
function truncates the fractional part, yielding12345.000000
. - The result is printed to the console.
Program Output:
Truncated value: 12345.000000