0

Why does this code print out a number?

#include <iostream> using namespace std; int main() { char zzz = 'z'; for (int i = 0; i< 10; i++) cout << zzz+i; // why does the line output a number? return 0; }

4th Aug 2020, 7:02 PM
Solus
Solus - avatar
2 Antworten
+ 3
char zzz is implicitly converted to an int when it is added to i. If you'd like it to output as a char you can cast it to a char. cout << (char)zzz+i;
4th Aug 2020, 7:17 PM
ChaoticDawg
ChaoticDawg - avatar
0
Got it. So a character will be converted to its decimal ASCII value when it is added to either an integer or another character.
4th Aug 2020, 8:13 PM
Solus
Solus - avatar