As per other replies, this is fine:
char c = ‘5’;
int x = c – ‘0’;
Also, for error checking, you may wish to check isdigit(c) is true first. Note that you cannot completely portably do the same for letters, for example:
char c = ‘b’;
int x = c – ‘a’; // x is now not necessarily 1
The standard guarantees that the char values for the digits ‘0’ to ‘9’ are contiguous, but makes no guarantees for other characters like letters of the alphabet.
Subtract ‘0’ like this:
int i = c – ‘0’;
The C Standard guarantees each digit in the range ‘0’..’9′ is one greater than its previous digit (in section 5.2.1/3 of the C99 draft). The same counts for C++.