Can someone explain why this works?
char c = '9'; int x = (int)(c - '0');
Why does subtracting '0' from an ascii code of a char result the number that that char is representing?
Because the char are all represented by a number and '0' is the first of them all.
On the table below you see that:
'0' => 48 '1' => 49 '9' => 57.
As a result: ('9' - '0') = (57 ? 48) = 9
'9'
'0'
Source: http://www.asciitable.com
2.1m questions
2.1m answers
60 comments
57.0k users