JuniorCodeOccasionalNot answered yet
What do c, c + 1, and char(c + 1) print?
Predict the output for char c = 'A'.
char c = 'A';
std::cout << c << " " << c + 1 << " " << char(c + 1);
Determine the output and explain.
A 66 B. c prints as the character A. In c + 1 the char is promoted to int, so the result is 66, printed as a number. Casting back with char(c + 1) makes it a char again, so the stream prints 'B' (ASCII 66).
- ✗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?