Please help me in understanding the following C Output:
#include<stdio.h>
int main() {
float x = 4.0;
printf("%f\n",x);
printf("%d\n",x);
int y=x;
printf("%d\n",y);
return 0;
}
Ouput on gcc compiler
4.000000
0
4
As far as i have read when we assign float to an int variable the decimal part of the variable is terminated and then assigned to the int.
Why it is not happening in this case?
You’re not printing
y, you’re printingxagain.As a side note,
printfcan’t do conversions. So passing a float when a%dis expected is undefined behavior.