printf("%c", ch); // print ch as a character
printf("%d\n", ch); // print the ASCII value of ch
printf("%d\n", 'ch'); // print the value of the multi-character literal 'ch'
// implementation defined, but in this case 'ch' == 'c' << 8 | 'h'
printf("%d", "ch"); // print the address of the string literal "ch"
// undefined behavior, read below
About multi-character literal read here
Your code invokes undefined behavior in the last printf
, since you're using the wrong format specifier. printf
is expecting an integer and you're passing an address. In 64-bit system this is most probably a 64-bit value while int
is 32 bits. The correct version should be
printf("%p", (void*)"ch");
Another problem is that you didn't use anything in iostream
, why include it? Don't include both iostream
and stdio.h
. Prefer iostream
in C++ because it's safer. If needed, use cstdio
instead of stdio.h
And you shouldn't tag both C and C++. They're different languages