Mixing doubles and integers?

J

Johs

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?
 
D

Dik T. Winter

> 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?
 
C

Chris Dollin

Johs said:
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.
 
M

mark_bluemel

Johs said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top