Using an incorrect format specifier in the first argument to printf
invokes undefined behavior.
For example, the code below invokes undefined behavior:
long z = 'B';
printf("%c\n", z);
Here is another example
printf("%f\n",0);
Above line of code is undefined behavior. %f
expects double. However 0 is of type int
.
Note that your compiler usually can help you avoid cases like these, if you turn on the proper flags during compiling (-Wformat
in clang
and gcc
). From the last example:
warning: format specifies type 'double' but the argument has type
'int' [-Wformat]
printf("%f\n",0);
~~ ^
%d