In C '0' + digit
is a cheap way of converting a single-digit integer into its character representation, like ASCII or EBCDIC. For example if you use ASCII
think of it as adding 0x30 ('0'
) to a digit.
The one assumption is that the character encoding has a contiguous area for digits - which holds for both ASCII and EBCDIC.
As pointed out in the comments this property is required by both the C++ and C standards. The C standard says:
5.2.1 - 3
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.