By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,507 Members | 1,758 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,507 IT Pros & Developers. It's quick & easy.

(part 23) Han from China answers your C questions

P: n/a
float and integers

c.***********@gmail.com said
consider following code snippet..
________________________
float a=3.0;
int b=1,c=2;
printf("%d %d %d ",a,b,c);
_________________________

above code give output
0 0 0
i can understand first zero as wrong format(%d) given for a,but i
could not get that why b and c are also printing as zero....
I will be making assumptions here about your system that will
infuriate the pedants, but I don't care. Given your X-HTTP-UserAgent
string, it is highly probable that these assumptions are valid.

Whether you have included stdio.h or not, the float argument to
printf() is promoted to a double, since even with a prototype
visible, the float falls into the variable-list part of the
variadic function printf().

On your system, a double has a 64-bit double-precision IEEE-754
floating-point representation, which means the float 3.0 will be
promoted and represented as 0x4008000000000000.

On your little-endian system, the 0x00 bytes will occur first
in memory.

On your system with 16-bit ints, the %d format specifiers will
operate on 0x0000, 0x0000, 0x0000.

To confirm my explanation, change
printf("%d %d %d ",a,b,c);
to
printf("%d %d %d %d",a,b,c);
and your output should be
0 0 0 16392
Yours,
Han from China

Nov 15 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Nov 15, 7:46*pm, Borked Pseudo Mailed <nob...@pseudo.borked.net>
wrote:
float and integers

c.lang.mys...@gmail.com said
consider following code snippet..
________________________
float a=3.0;
int b=1,c=2;
printf("%d %d %d ",a,b,c);
_________________________
above code give output
0 0 0
i can understand first zero as wrong format(%d) given for a,but i
could not get that why b and c are also printing as zero....

I will be making assumptions here about your system that will
infuriate the pedants, but I don't care. Given your X-HTTP-UserAgent
string, it is highly probable that these assumptions are valid.

Whether you have included stdio.h or not, the float argument to
printf() is promoted to a double, since even with a prototype
visible, the float falls into the variable-list part of the
variadic function printf().

On your system, a double has a 64-bit double-precision IEEE-754
floating-point representation, which means the float 3.0 will be
promoted and represented as 0x4008000000000000.

On your little-endian system, the 0x00 bytes will occur first
in memory.

On your system with 16-bit ints, the %d format specifiers will
operate on 0x0000, 0x0000, 0x0000.

To confirm my explanation, change
* * printf("%d %d %d ",a,b,c);
to
* * printf("%d %d %d %d",a,b,c);
and your output should be
* * 0 0 0 16392 *

Yours,
Han from China
Yes,it is showing same o/p as you explained...
It is now clear to me....
Nov 15 '08 #2

P: n/a
On Sat, 15 Nov 2008 08:59:27 -0800 (PST), c.***********@gmail.com wrote:
On Nov 15, 7:46*pm, Borked Pseudo Mailed <nob...@pseudo.borked.net>
wrote:
>to
* * printf("%d %d %d %d",a,b,c);
and your output should be
* * 0 0 0 16392 *

Yours,
Han from China

Yes,it is showing same o/p as you explained...
It is now clear to me....
If this is shorthand for output, it is ambiguous. Lately I write -o x.exe.
Before that I wrote -o out.

OP is the original poster.

An unskillful negating of quoting ettiquette is the mark of a troll. If I
were tasked to pluck ten persons out of clc who didn't turn to stone when
asked a practical question, Han would be on my long list. The authors of
unleashed would be on my short list. Ben would be on top, if I didn't have
to recognize a talent I've never read: Lawrence Jones.
--
George

Faith crosses every border and touches every heart in every nation.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
Nov 16 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.