Skip to main content

Problem #3

Posted in

int var = 7;
printf("%f\n", var);

Q: What will this print?
A: Unpredictable behavior

Explanation: All functions which accept a format and a list of parameters of variable length are not good in handling input parameters' types. Here, printf doesn't know that var is of integer type. What he knows (or it is better to say trusts us) is that var should be treated as a floating point number, because we have specified %f flag. So, the behavior will be unpredictable, since these types have different representation (I mean low level representation). You will get a warning at compilation, but if the compilation output is huge we might not notice it at all.

Lesson to learn: When using such functions, make sure you there is no inconistency between input parameter, and format flags.

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
in_ormati_n: