470,867 Members | 1,793 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,867 developers. It's quick & easy.

Mixing doubles and integers?

In the below example a double is initialized with an integer. Afterwards
another double is added:

int mini = 0;
double dd = 7;
double a = mini;
double res = a + dd;

printf("res = %d\n", res);

res equals 0, but why does it not equal 7?
Jan 3 '07 #1
3 1237
In article <en**********@news.net.uni-c.dkJohs <sd*@sdf.comwrites:
In the below example a double is initialized with an integer. Afterwards
another double is added:

int mini = 0;
double dd = 7;
double a = mini;
double res = a + dd;

printf("res = %d\n", res);

res equals 0, but why does it not equal 7?
Are you sure the format specifier matches the argument to printf?
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Jan 3 '07 #2

Johs wrote:
In the below example a double is initialized with an integer. Afterwards
another double is added:

int mini = 0;
double dd = 7;
double a = mini;
double res = a + dd;

printf("res = %d\n", res);

res equals 0, but why does it not equal 7?
You don't know what it equals - you lied to printf() and told it you
were passing an int, then gave it a double...

Note - it's usually better to cut and paste a complete testcase program
(small enough to be postable) rather than just some code snippets like
these.

Jan 3 '07 #3
Johs wrote:
In the below example a double is initialized with an integer. Afterwards
another double is added:

int mini = 0;
double dd = 7;
double a = mini;
double res = a + dd;

printf("res = %d\n", res);

res equals 0, but why does it not equal 7?
`res` is a double. `%d` format expects an int. Your
code is broken: it will exhibit Undefined Behaviour,
which allows any of: behaving the way you want,
behaving the way /I/ want, behaving the way the
compiler-writer wanted, behaving the way the printf-
writer wanted, behaving the way that "came naturally"
for the code of the compiler and/or printf; such
behaviour including, but not limited to, printing
"rubbish", printing rubbish, printing a helpful
run-time diagnostic, printing a helpful compile-time
diagnostic, printing an /un/helpful run-time diagnostic,
printing nothing but corrupting memory so that a completely
obscure and apparently unrelated misbehaviour happens
later on, ditto but not until an important client
demo, ditto but not until used in a life-critical
application [1], posting your credit card details to
a selection of blogs, wiping your hard drive, and
teleporting Early Spike into your bedroom late at
night. Yes, the Standard doesn't even require Undefined
Behaviour to be physically possible: it doesn't require
anything at all. Hedgehogs may sing.

[1] Of course the code reviews and unit tests will
catch this before production [2].

[2] Nervous giggle.

--
Chris "hopefully not Pyecroft" Dollin
Nit-picking is best done among friends.

Jan 3 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by christine | last post: by
4 posts views Thread by Graham | last post: by
5 posts views Thread by Stef | last post: by
25 posts views Thread by Allan Rydberg | last post: by
68 posts views Thread by Martin Joergensen | last post: by
3 posts views Thread by frank | last post: by
2 posts views Thread by utab | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.