All data in a computer are stored as numbers. This is true of characters
like 'A', '@', ' ', and
'4'. Every character has a corresponding integer code. Different
computers may have different encoding schemes, so the code number
corresponding to the character 'A' may be 65 on some machines and
193 on others. The C++ standard does not dictate which encoding scheme a
system must use.
What the C++ standard does dictate is that the code numbers for the
digit characters '0' through '9' must be contiguous
and increasing. In other words, for any particular encoding scheme, there is
an integer x such that
'0' has code number x'1' has code number x+1'2' has code number x+2'9' has code number x+9For the ASCII encoding scheme, x happens to be 48, while for the EBCDIC scheme, it's 240. However, we should never need to know the particular value for x. Let's see why.
C and C++ have the following rules: If a char is used where a number is required, the compiler causes the program to use the code number for that character; if a number is used where a char is required, the program uses the char corresponding to that code number. So consider this:
char ch = '0'; // Let's say the code number for '0' is x
ch++; // now ch is '1' (x+1)
ch += 7; // now ch is '8' (since (x+1)+7 is x+8)
int n = ch; // n is the code number for '8' (i.e., x+8)
int m = ch - '3'; // '8' - '3'
// which is (x+8) - (x+3)
// which is 8 - 3
// so m is 5
Notice that m will be 5 no matter what encoding scheme our
computer uses (so no matter what value x, the code for
'0', is). If it's ASCII, for example, ch ends up
as 56 and '3' is 51, and 56 minus 51 is 5. If it's EBCDIC,
ch is 248 and '3' is 243, and 248 minus 243 is
5. In general, your program should never need to use integer constants
like 48 to represent a character code; if you want to represent the code
for the character '0', for example, just say
'0', not 48.
On another note, why do we interpret the sequence of characters consisting of the digit four, the digit seven, and the digit three as the number four hundred seventy-three? What if someone revealed the digits to you one at a time?
"I'm going to show you some digits, and you tell me what number they
represent. The first digit is four."
"OK, I think the number is four."
"The next digit is seven."
"Hmmm, I thought the number was four, but there's another digit. Ten times
four is forty, plus seven is forty-seven. I think the number is
forty-seven."
"The next digit is three."
"Hmmm, I thought the number was forty-seven, but there's another digit. Ten
times forty-seven is four hundred seventy, plus three is four hundred
seventy-three. I think the number is four hundred seventy-three."
"There are no more digits."
"The number is four hundred seventy-three."