0

I cant understand why result is that

#include <iostream> using namespace std; int main() { char a= 'b'; char b= 'a'; cout << b%a; return 0; } why output is 97 ?

2nd Aug 2017, 5:27 PM
ChaTho
ChaTho - avatar
5 odpowiedzi
+ 2
cout << 'a' % 'b'; // 97 // same as: cout << 97 % 98; // 97 // because: cout << (int) 'a'; // 97 cout << (int) 'b'; // 98 // or: cout << (char) 97; // 'a' cout << (char) 98; // 'b' The numbers you get are the positions of these chars in the ASCII table.
2nd Aug 2017, 6:01 PM
Boris Batinkov
Boris Batinkov - avatar
2nd Aug 2017, 6:05 PM
Hatsy Rei
Hatsy Rei - avatar
+ 2
Because characters are threatened as integers while calculating with them.
2nd Aug 2017, 5:35 PM
Jonas Schröter
Jonas Schröter - avatar
+ 1
to see ASCII codes: #include <cstdio> // for printf() int main(){ for(int i = 1; i < 256; ++i) printf("%i = %c\n", i, i); }
2nd Aug 2017, 11:02 PM
lion
lion - avatar
0
isnt there an easy way to know ASCII numbers ?
2nd Aug 2017, 7:02 PM
ChaTho
ChaTho - avatar