should one cast all
printf
parameters to be sure that they really are of the expected type?
Not generally. You should use the correct combination of format specifier and argument type for each argument you are passing. Of course, if you have an argument that is the "wrong type" (e.g. an int
that you want to print using a floating point format specifier) then it needs to be converted in some way, which would typically mean a cast. But scattering a load of casts all over your printf
lines "just in case" is not the right solution here.
Note that there is no requirement for the compiler to "understand" the format string of printf
(or scanf
or strftime
, etc), so the compiler is just obliged to pass arguments according to a set list of restrictions (float
is converted to double
, short integers (char
, short
) are converted to int
, and a few other things like that). Then it comes down to the printf
to sort out what you have. And of course, if you don't have the right format specification for the argument, then printf
may indeed look in the wrong place for the arguments.
So, in summary, you need to match the argument type for printf
. If that means that you occasionally need a cast, then yes, use a cast. But it shouldn't be a "regular thing".