Interview Question
Country: India
It's undefined behaviour...
In practice, what you're seeing is probably due to the fact that %f makes printf pull a double from its argument list, which is 8 bytes*. But d is only 4 bytes in size, so now everything is misaligned.
Remember that printf is a variadic function, which means it's completely non-type-safe; printf has to extract raw bytes from the stack without any help from the compiler. It's entirely up to you to ensure that the arguments correspond precisely to the format string.
You need to study Danish Ritchie......
You can read floats, or doubles, with "%f" and "%lf", in scanf(), but in printf(), "%f" prints doubles.)
You mean Dennis Ritchie? I know India's getting a lot of computer scientists these days, but seriously, come on, this guy's from wayyy in the day.
here float value is explicitly typecasted in integer and compiler prefer explicit over implicit so it takes the integer value and it can not be furthure promoted to float.
int main()
{
float a;
scanf("%f",&a);
a = (float)(int)a;
getchar();
printf("%f",a);
getchar();
return 0;
}
Acc to u o/p of abv prg should b 0.0000
no it will typecast the float value into an integer then again integer will b converted to float value n we r using %f so it will print that float value
For example on prompt from "scanf("%f",&a);" we will write "2.6"
- zprogd June 15, 2012Float variable "a=2.6" looks as hex 0x40266666
or in binary
01000000001001100110011001100110
Here:
0 - sign
10000000 - exponent (1 + 127 = 128)
1001100110011001100110 - mantissa
Number 0x40266666 in the little-endian memory placed as <66 66 26 40>, here 40 include the exponent.
When we call function printf() without typecast in integer
printf("%f",a);
Compiler translate 32-bit float into 64 bit double. In the little-endian memory our double "2.6" looks as <00 00 00 c0 cc cc 04 40>
Here last <04 40> include the exponent. I'm lazy to explain here the binary representation double in memory.
When we call printf() with typecast in integer
printf("%f",(int)a);
Compiler translate 32-bit float into 32 bit int. Float 2.6 will be integer 2. In the litlle-endian memory our integer "2" looks as <02 00 00 00>
Then printf() parses format string "%f". It wants 64-bit double <00 00 00 c0 cc cc 04 40> but instead it gets <02 00 00 00 00 00 00 00>. I don't know why there is <00 00 00 00> after <02 00 00 00>. On my system this "0" is result "push edi" when system saves registers in the begin current procedure. I think this feature is very system dependent.
Any way function printf() gets <02 00 00 00 00 00 00 00>. In this case exponent bits are allways "0" i.e. exponent always is -1023. This is very-very small number with a lot of zeroes after point even if we will use the max possible integer 0x7FFFFFFF.
The format string "%f" prints string with only 8 characters length and can't print all zeroes.
Another way if you change precision in format string as below
printf("%.340f",(int)a);
And it's wonderful! You will look something like this:
0.0000000000000000000000000000000000000000
000000000000000000000000000000000000000000
000000000000000000000000000000000000000000
000000000000000000000000000000000000000000
000000000000000000000000000000000000000000
000000000000000000000000000000000000000000
000000000000000000000000000000000000000000
000000000000000000000000000000098813129168
249309