Interview Question


Country: India




Comment hidden because of low score. Click to expand.
3
of 3 vote

For example on prompt from "scanf("%f",&a);" we will write "2.6"
Float 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

- zprogd June 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

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.

- Arya June 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That's not right. %f is for a float; %lf is for a double

- Anonymous June 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You need to study Danish Ritchie......

You can read floats, or doubles, with "%f" and "%lf", in scanf(), but in printf(), "%f" prints doubles.)

- Arya June 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Anonymous June 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Looks like you might be right about %f being OK to print doubles, but it's also perfectly OK to use it to print floats.

- Anonymous June 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- kuldeep June 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- shani June 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- kuldeep June 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

firstly , explicit conversion wud convrt float to int.... its gonna take the integer part( before dot.)... then this is finally converted to float by %f..... moreover , its concept of "floating point emulator"... that is third party software.. not of c..

- san June 14, 2012 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More