What happens when I use the wrong format specifier in C?
Generally speaking, the behavior is undefined. *
However, recall that printf is a variational function and that arguments for variational functions are subject to default promotions. So, for example, a char advances to int . Therefore, in practice, they will give the same results:
char x = 'A'; printf("%c\n", x); int y = 'A'; printf("%c\n", y);
whereas this behavior is undefined:
long z = 'A'; printf("%c\n", z);
* See, for example, section 7.19.6.1 p9 of standard C99:If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined. Sub>
source share