10 Beginner The Hundred
char + int = int
one small step for a letter
#include <iostream>
int main() {
char letter = 'a';
std::cout << "after " << letter << " comes " << letter + 1 << '\n'; // next letter, surely
}
Run it. What does it print?
Answer
after a comes 98. The + quietly turned the letter into a number.
Why¶
Doing arithmetic on a char triggers integer promotion: the char becomes an int
before the + ever happens. So letter + 1 is an int with value 98 ('a' is 97 in
ASCII), and operator<< picks the int overload, which prints digits. The variable's
type does not survive the expression — even 'a' + 'a' is an int (194, not some
double-wide letter). This rule is inherited from C, applies to every small integer type,
and draws no warning even with -Wall -Wextra.
The fix¶
Cast the result back when you want a character:
The same promotion is also a deliberate idiom: std::cout << +letter; (unary plus) is
the tidy way to print a char's numeric value — it prints 97.
Takeaway: char survives storage, not arithmetic — the moment you do math on it,
you're holding an int.
Try it: g++ -std=c++17 main.cpp -o demo && ./demo