printf
is unusual in that it has no single function signature. In other words, there’s no mechanism to automatically take the actual arguments you pass and convert them into the type that’s expected. Whatever you try to pass, gets passed. If the type you pass does not match the type expected by the corresponding format specifier, you get nonsense.
Furthermore, if the mismatched types you’re passing versus what the format specifiers are expecting have different sizes, printf
can end up popping random garbage off the stack, that has nothing to do with what you thought you passed.
You asked, “doesn’t it have to be deterministic?”, and the answer there is a very emphatic “NO“! By passing the wrong type of value to printf
, you have invoked undefined behavior, which means that anything can happen, and it very definitely does not have to be the same thing twice.
(Even though they’re not required to, some compilers peek at the format string and try to check the values you passed against the values that are expected. For example, under gcc, your first code fragment gets the warning “format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’”. But I don’t know of a compiler that will try to do conversions to “fix” the arguments.)
1
solved c-behaviour of printf(“%d”,