Just wondering what happens when I use the wrong format specifier in C?
For example:
x = \'A\';
printf(\"%c\\n\", x);
printf(\"%d\\n\", x);
x = 65;
what happens when I use the wrong format specifier in C?
Generally speaking, undefined behaviour.*
However, recall that printf
is a variadic function, and that the arguments to variadic functions undergo the default argument promotions. So for instance, a char
is promoted to an int
. So in practice, these will both give the same results:
char x = 'A';
printf("%c\n", x);
int y = 'A';
printf("%c\n", y);
whereas this is undefined behaviour:
long z = 'A';
printf("%c\n", z);
If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.