Because the integral values of the digit characters are guaranteed by the C standard to be consecutive.
Therefore '1' - '0' == 1
, '2' - '0' == 2
, etc. from which you can infer that your formulas really do work.
Sidenotes:
- Since this is guaranteed by the standard, it works even if the target platform does not use ASCII.
- Conversely, if the standard did not mandate this (it does not do so with the values of the letters) then this technique would not be portable; it would be dependent on the target system using ASCII.