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

a simple query

P: n/a
printf("%d",(float)3/2);
the output comes as zero...why not 1?even when u change it to
printf("%d",(float)6/2); still the output is zero

Mar 9 '07 #1
Share this Question
Share on Google+
18 Replies


P: n/a
newbai wrote:
printf("%d",(float)3/2);
the output comes as zero...why not 1?even when u change it to
printf("%d",(float)6/2); still the output is zero
Who's u?

%d is telling printf you are passing an integer value, but you are
passing a float, that's undefined behaviour.

--
Ian Collins.
Mar 9 '07 #2

P: n/a
newbai wrote:
printf("%d",(float)3/2);
the output comes as zero...why not 1?even when u change it to
printf("%d",(float)6/2); still the output is zero
The %d format specifier is for printing int arguments. Giving printf
the wrong type will cause undefined behaviour, i.e. anything can
happen.

Do:

printf("%d\n", 3/2); and printf("%d\n", 6/2);

or

printf("%f\n", (float)3/2); and printf("%f\n", (float)6/2);

Mar 9 '07 #3

P: n/a
newbai wrote:
printf("%d",(float)3/2);
the output comes as zero...why not 1?even when u change it to
printf("%d",(float)6/2); still the output is zero
%d wants an int, the expression is a float, all bets are off.

BOOM tomorrow. There's always a BOOM tomorrow.

--
Chris "Ivanova" Dollin
"Who do you serve, and who do you trust?" /Crusade/

Mar 9 '07 #4

P: n/a
In article <11*********************@v33g2000cwv.googlegroups. com"newbai" <si**************@gmail.comwrites:
printf("%d",(float)3/2);
the output comes as zero...why not 1?even when u change it to
printf("%d",(float)6/2); still the output is zero
Try %f.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Mar 9 '07 #5

P: n/a
newbai wrote:
>
printf("%d",(float)3/2);
the output comes as zero...why not 1?even when u change it to
printf("%d",(float)6/2); still the output is zero
Use the normal adage "A cast is probably an error" and then see
what you get. In addition, avoid the childish use of silly
abbreviations, such as 'u'.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Mar 9 '07 #6

P: n/a

"CBFalconer" <cb********@yahoo.comwrote in message
newbai wrote:
>>
printf("%d",(float)3/2);
the output comes as zero...why not 1?even when u change it to
printf("%d",(float)6/2); still the output is zero

Use the normal adage "A cast is probably an error" and then see
what you get. In addition, avoid the childish use of silly
abbreviations, such as 'u'.
It is also very rude to leave your message unsigned.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Mar 9 '07 #7

P: n/a
"Malcolm McLean" <re*******@btinternet.comwrites:
"CBFalconer" <cb********@yahoo.comwrote in message
>newbai wrote:
>> printf("%d",(float)3/2);
the output comes as zero...why not 1?even when u change it to
printf("%d",(float)6/2); still the output is zero
Use the normal adage "A cast is probably an error" and then see
what you get. In addition, avoid the childish use of silly
abbreviations, such as 'u'.
It is also very rude to leave your message unsigned.
Is it? I don't agree. As long as your name (or something by which we
can refer to you) is in the headers, I don't think a signature is
strictly necessary.

I do wish that more people would put a "real name" (whether it's their
legal name or just a handle) in the "From: " header, rather than just
an e-mail address.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 10 '07 #8

P: n/a
Keith Thompson wrote:
"Malcolm McLean" <re*******@btinternet.comwrites:
>"CBFalconer" <cb********@yahoo.comwrote in message
>>newbai wrote:

printf("%d",(float)3/2);
the output comes as zero...why not 1?even when u change it to
printf("%d",(float)6/2); still the output is zero

Use the normal adage "A cast is probably an error" and then see
what you get. In addition, avoid the childish use of silly
abbreviations, such as 'u'.
It is also very rude to leave your message unsigned.

Is it? I don't agree. As long as your name (or something by which we
can refer to you) is in the headers, I don't think a signature is
strictly necessary.

I do wish that more people would put a "real name" (whether it's their
legal name or just a handle) in the "From: " header, rather than just
an e-mail address.
Besides which the message to which the good M.McLean responded
carried a sig. line. He seems to have some software difficulties.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Mar 10 '07 #9

P: n/a
Keith Thompson said:

<snip>
I do wish that more people would put a "real name" (whether it's their
legal name or just a handle) in the "From: " header, rather than just
an e-mail address.
Because it makes your reply easier and quicker to construct? Well then,
that's easily fixed - simply *don't reply* to people who construct
their headers or articles in a way that is not in line with Usenet and
comp.lang.c conventions. Let others take the strain of explaining the
conventions to such folks - heaven knows there's no shortage of people
here who are prepared to do that! - and save your time for those who
value it.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Mar 10 '07 #10

P: n/a
Richard Heathfield <rj*@see.sig.invalidwrites:
Keith Thompson said:

<snip>
>I do wish that more people would put a "real name" (whether it's their
legal name or just a handle) in the "From: " header, rather than just
an e-mail address.

Because it makes your reply easier and quicker to construct? Well then,
that's easily fixed - simply *don't reply* to people who construct
their headers or articles in a way that is not in line with Usenet and
comp.lang.c conventions. Let others take the strain of explaining the
conventions to such folks - heaven knows there's no shortage of people
here who are prepared to do that! - and save your time for those who
value it.
Well, that particular thing doesn't make any real difference in terms
of constructing a reply. For example, Paul Hsieh's articles' "From: "
headers say "we******@gmail.com" rather than
"Paul Hsieh <we******@gmail.com>", but when I reply to one of his
articles I just don't bother inserting his real name into the
attribution line. As far as I know, including a real name in the
"From: " header isn't even a requirement; it just makes it a bit
easier to follow the conversation. There are far worse things than
leaving it out.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 10 '07 #11

P: n/a
Richard Heathfield wrote:
Keith Thompson said:

<snip>
>I do wish that more people would put a "real name" (whether it's
their legal name or just a handle) in the "From: " header, rather
than just an e-mail address.

Because it makes your reply easier and quicker to construct? Well
then, that's easily fixed - simply *don't reply* to people who
construct their headers or articles in a way that is not in line
with Usenet and comp.lang.c conventions. Let others take the strain
of explaining the conventions to such folks - heaven knows there's
no shortage of people here who are prepared to do that! - and save
your time for those who value it.
Besides which the 'from' doesn't really matter for replies if you
take the trouble to configure the 'reply-to' field. That field is
relatively immune to spammer scanning, yet makes replies fully
automatic (on any decent newsreader - not sure if google heeds it).

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews

--
Posted via a free Usenet account from http://www.teranews.com

Mar 10 '07 #12

P: n/a
"CBFalconer" <cb********@yahoo.comschrieb im Newsbeitrag
news:45***************@yahoo.com...
Richard Heathfield wrote:
>Keith Thompson said:

<snip>
>>I do wish that more people would put a "real name" (whether it's
their legal name or just a handle) in the "From: " header, rather
than just an e-mail address.

Because it makes your reply easier and quicker to construct? Well
then, that's easily fixed - simply *don't reply* to people who
construct their headers or articles in a way that is not in line
with Usenet and comp.lang.c conventions. Let others take the strain
of explaining the conventions to such folks - heaven knows there's
no shortage of people here who are prepared to do that! - and save
your time for those who value it.

Besides which the 'from' doesn't really matter for replies if you
take the trouble to configure the 'reply-to' field. That field is
relatively immune to spammer scanning, yet makes replies fully
automatic (on any decent newsreader - not sure if google heeds it).
How spam save is the reply-to?

Bye, Jojo
Mar 10 '07 #13

P: n/a
On Mar 8, 8:47 pm, "newbai" <siddhakudchad...@gmail.comwrote:
printf("%d",(float)3/2);
the output comes as zero...why not 1?even when u change it to
printf("%d",(float)6/2); still the output is zero
The C programming language does not perform type checking in many
situations including this one when parameters are passed through a
"..." prototype. So the (float)3/2 is passed as a real floating
parameter. However, "%d" causes the library function to read the
parameter as if it were an int. The result is kind of like doing
this:

float x = 1.0;
int y = *((int *)&x);

which just reads memory that's representing a floating point as if it
were an integer, which doesn't have any defined meaning in ANSI C.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Mar 10 '07 #14

P: n/a
we******@gmail.com writes:
On Mar 8, 8:47 pm, "newbai" <siddhakudchad...@gmail.comwrote:
> printf("%d",(float)3/2);
the output comes as zero...why not 1?even when u change it to
printf("%d",(float)6/2); still the output is zero

The C programming language does not perform type checking in many
situations including this one when parameters are passed through a
"..." prototype. So the (float)3/2 is passed as a real floating
parameter.
Right. Furthermore, an argument of type float is promoted to type
double when passed through a "..." prototype.
However, "%d" causes the library function to read the
parameter as if it were an int. The result is kind of like doing
this:

float x = 1.0;
int y = *((int *)&x);

which just reads memory that's representing a floating point as if it
were an integer, which doesn't have any defined meaning in ANSI C.
It's kind of like that, but worse. The "%d" tells printf to read an
int argument; the double argument that you actually passed isn't
necessarily even in the same place that the int argument would have
been. For example, the calling convention might use different
registers for integer and floating-point arguments. (That's not too
likely in this case, since variadic arguments are typically passed in
the same way regardless of type (typically on "the stack" for machines
that use a stack), but the standard allows it.)

The bottom line is that it's undefined behavior; the solution is not
to do that.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 10 '07 #15

P: n/a
Joachim Schmitz wrote:
"CBFalconer" <cb********@yahoo.comschrieb im Newsbeitrag
.... snip ...
>>
Besides which the 'from' doesn't really matter for replies if you
take the trouble to configure the 'reply-to' field. That field is
relatively immune to spammer scanning, yet makes replies fully
automatic (on any decent newsreader - not sure if google heeds it).

How spam save is the reply-to?
I can't quantify it, but my experience over the past 5 years or so
indicates it is quite safe. The yahoo account gets lots of spam,
but not my reply-to account.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews

--
Posted via a free Usenet account from http://www.teranews.com

Mar 11 '07 #16

P: n/a
"CBFalconer" <cb********@yahoo.comschrieb im Newsbeitrag
news:45***************@yahoo.com...
Joachim Schmitz wrote:
>"CBFalconer" <cb********@yahoo.comschrieb im Newsbeitrag
... snip ...
>>>
Besides which the 'from' doesn't really matter for replies if you
take the trouble to configure the 'reply-to' field. That field is
relatively immune to spammer scanning, yet makes replies fully
automatic (on any decent newsreader - not sure if google heeds it).

How spam save is the reply-to?

I can't quantify it, but my experience over the past 5 years or so
indicates it is quite safe. The yahoo account gets lots of spam,
but not my reply-to account.
Good enough for me, thanks!

Bye, Jojo
Mar 11 '07 #17

P: n/a
CBFalconer <cb********@yahoo.comwrote:
Richard Heathfield wrote:
Keith Thompson said:
I do wish that more people would put a "real name" (whether it's
their legal name or just a handle) in the "From: " header, rather
than just an e-mail address.
Because it makes your reply easier and quicker to construct? Well
then, that's easily fixed - simply *don't reply* to people who
construct their headers or articles in a way that is not in line
with Usenet and comp.lang.c conventions. Let others take the strain
of explaining the conventions to such folks - heaven knows there's
no shortage of people here who are prepared to do that! - and save
your time for those who value it.

Besides which the 'from' doesn't really matter for replies if you
take the trouble to configure the 'reply-to' field.
And we should not reply at all to people who have neither a valid From:
nor a valid Reply-to:? Yes, I can live with that.

Richard
Mar 12 '07 #18

P: n/a
Keith Thompson wrote, On 10/03/07 21:58:
we******@gmail.com writes:
>On Mar 8, 8:47 pm, "newbai" <siddhakudchad...@gmail.comwrote:
>> printf("%d",(float)3/2);
the output comes as zero...why not 1?even when u change it to
printf("%d",(float)6/2); still the output is zero
The C programming language does not perform type checking in many
situations including this one when parameters are passed through a
"..." prototype. So the (float)3/2 is passed as a real floating
parameter.

Right. Furthermore, an argument of type float is promoted to type
double when passed through a "..." prototype.
> However, "%d" causes the library function to read the
parameter as if it were an int. The result is kind of like doing
this:

float x = 1.0;
int y = *((int *)&x);

which just reads memory that's representing a floating point as if it
were an integer, which doesn't have any defined meaning in ANSI C.

It's kind of like that, but worse. The "%d" tells printf to read an
int argument; the double argument that you actually passed isn't
necessarily even in the same place that the int argument would have
been. For example, the calling convention might use different
registers for integer and floating-point arguments. (That's not too
likely in this case, since variadic arguments are typically passed in
the same way regardless of type (typically on "the stack" for machines
that use a stack), but the standard allows it.)
Actually, MS does pass the first few varargs parameters in registers
with some of its calling conventions. Floating point arguments are
passed in both floating point and integer registers (with the numbers
converted to integers in the integer registers), but integer parameters
are only passed in integer registers. So you really can hit problems of
this sort on some real implementations.
The bottom line is that it's undefined behavior; the solution is not
to do that.
Yes.
--
Flash Gordon
Mar 12 '07 #19

This discussion thread is closed

Replies have been disabled for this discussion.