Why does subtracting '0' in C result in the number that the char is representing?

Solution 1:

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

enter image description hereSource: http://www.asciitable.com

Solution 2:

char is an integer type, just like int and family. An object of type char has some numerical value. The mapping between characters that you type in a character literal (like '0') and the value that the char object has is determined by the encoding of that character in the execution character set:

  • C++11 §2.14.3:

    An ordinary character literal that contains a single c-char representable in the execution character set has type char, with value equal to the numerical value of the encoding of the c-char in the execution character set.

  • C99 §6.4.4.4:

    An integer character constant is a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'.

    [...]

    An integer character constant has type int.

    Note that the int can be converted to a char.

The choice of execution character set is up to the implementation. More often than not, the choice is ASCII compatible, so the tables posted in other answers have the appropriate values. However, the character set does not need to be ASCII compatible. There are some restrictions, though. One of them is as follows (C++11 §2.3, C99 §5.2.1):

a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
0 1 2 3 4 5 6 7 8 9
_ { } [ ] # ( ) < > % : ; . ? * + - / ^ & | ~ ! = , \ " ’

[...]

In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.

This means that whatever value the character '0' has, the character '1' has value one more than '0', and character '2' has value one more than that, and so on. The numeric characters have consecutive values. You can summarise the mapping like so:

Character:            0    1    2    3    4    5    6    7    8    9
Corresponding value:  X    X+1  X+2  X+3  X+4  X+5  X+6  X+7  X+8  X+9

All of the digit characters have values offset from the value of '0'.

That means, if you have a character, let's say '9' and subtract '0' from it, you get the "distance" between the value of '9' and the value of '0' in the execution character set. Since they are consecutive, the distance will be 9.

Solution 3:

Because the C standard guarantees that the characters 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 are always in this order regarding their numerical character code. So, if you subtract the char code of '0' from another digit, it will give its position relative to 0, which is its value...

From the C standard, Section 5.2.1 Character sets:

In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous

Solution 4:

Because, the literals are arranged in sequence.

So if 0 was 48, 1 will be 49, 2 will be 50 etc.. in ASCII, then x would contain, ascii value of '9' minus the ascii value of '0' which means, ascii value of '9' would be 57 and hence, x would contain 57 - 48 = 9.

Also, char is an integral type.

Solution 5:

the code ascii of numeric chars are ordered in the order '0' '1' '2' '3' '4' '5' '6' '7' '8' '9' as indicated in the ascii table

so if we make difference beween asii of '9' and ascii of '0' we will get 9