473,378 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

a simple query

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
18 1604
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
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
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
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
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

"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
"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
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
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
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
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
"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
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
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: unixman | last post by:
As usual, it is 2:00am, and I'm pulling my hair out, finally resorting to posting in the newsgroups for help. :) Simple problem, in theory. Given table "map": CREATE TABLE map ( entry_id...
6
by: Eddie Smit | last post by:
field- field- surname town ---- ---- john NY john Vegas john Boston eddie Boston eddie New Orleans eddie NY
2
by: Mrs Howl | last post by:
I have a query that just reads one table and appends to an output table, one-for-one. No criteria. It's not a Total query (i.e. no group by). It normally run run in minutes, but gets horribly...
1
by: j.mandala | last post by:
I created a simple link between two tables in a query. TableA has Social Security numbers stored as Long Integer Data. (I imported this table). The Join is between these two fields Table ...
3
by: Don Sealer | last post by:
I'm guessing this is pretty simple however not simple enough for me. I'm developing a database to track expenses, income, banking transactions, etc. I have a very simple query with four fields,...
2
by: Don Wash | last post by:
Hi All! I've been searching everywhere for a simple sample of producing a bar graph using CrystalReport by specifying SQL Query, and I've found none of it! I find so many complex samples with so...
3
by: John Baker | last post by:
Hi:7 Newby here to ASP, and using the ASP.NET Web Matrix development tool. While that tool looks great for a Newby, I have run into a snag. I have an HTML Text Box which I have named HireInput,...
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
2
by: Fendi Baba | last post by:
I created a person table with various fields such as Suffix, Salutation, etc, Some of these fields may not be mandatory for example suffix. In the actual table itself, I only have a field for...
9
by: Emin | last post by:
Dear Experts, I have a fairly simple query in which adding a where clause slows things down by at least a factor of 100. The following is the slow version of the query ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.