C Programming - Floating Point Issues - Discussion

Discussion Forum : Floating Point Issues - Find Output of Program (Q.No. 3)
3.
What will be the output of the program?
#include<stdio.h>
int main()
{
    float fval=7.29;
    printf("%d\n", (int)fval);
    return 0;
}
0
0.0
7.0
7
Answer: Option
Explanation:

printf("%d\n", (int)fval); It prints '7'. because, we typecast the (int)fval in to integer. It converts the float value to the nearest integer value.

Discussion:
6 comments Page 1 of 1.

Chirag said:   4 years ago
What exactly is fval?

Uma said:   8 years ago
Please anybody clearly explain.

Prajyot said:   9 years ago
It never converts the float value to the "nearest" integer value. It just a floor value of float.

#include<stdio.h>
int main()
{
float fval=7.99;
printf("%d\n", (int)fval);
return 0;
}

o/p: 7.

Ekta said:   1 decade ago
If I write it like this:
#include<stdio.h>
int main()
{
float fval=7.29;
printf("%d\n",fval);
return 0;
}

Output is -1073741824 How explain it?

Harsh said:   1 decade ago
Yes you are right whenever float is type cast to int, value is truncated not round off. Truncate means simple "drop the decimal part".

Jack swagger said:   1 decade ago
I think it truncates the decimal values so the data before decimal point is printed,but it wont round it up.

Post your comments here:

Your comments will be displayed after verification.