Back to questionsWhat do
JuniorCodeOccasional
What do c, c + 1, and char(c + 1) print?
Predict the output for char c = 'A' on an ASCII-based platform.
char c = 'A';
std::cout << c << " " << c + 1 << " " << char(c + 1);Determine the output and explain.
A 66 B with ASCII, where 'A' is 65 (the encoding is implementation-defined). c prints as a character; in c + 1 the char is promoted to int, so 66 prints as a number; char(c + 1) is a char again: B.
- Thinking char arithmetic stays char-typed
- Believing streaming a char prints its numeric code
- Missing that the explicit char cast changes how the stream formats it
- Why does
std::cout << cprint a letter butstd::cout << c + 1print a number? - What integer type does a
charpromote to in arithmetic?