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.