1

I'm reading the C programming textbook1 and in 1.6 they're saying the conversion between a char containing a digit and an int can be done like this:

char character = '7';
int integerChar = character - '0';

I'm having trouble understanding what is happening here and why the integer value is equal to the character minus the character '0'.


1 Brian W Kernighan and Dennis M Ritchie The C Programming Language, 2nd Edn 1988.

  • You might want to do a web search on "ASCII table"... – DevSolar Mar 01 '23 at 16:20
  • The character values of `'0'` through `'9'` are consecutive. So `'1'` is `'0' + 1, etc. Subtracting `'0'` gives the displacement from the character `'0'`, which is the integer represented by the character. `'0' - '0'` is obviously zero, `'1' - '0'` is `('0' + 1) - '0'`, which is one, etc. – Tom Karzes Mar 01 '23 at 16:22
  • Also, when not packed into an array, one normally uses an `int` to hold a character value. – Tom Karzes Mar 01 '23 at 16:23

3 Answers3

4

From the C Standard (5.2.1 Character sets)

  1. ...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.

So it means that the Standard guarantees that for example the difference '1' - '0' is equal to 1, or the difference '2' - '0' is equal to 2 and so on.

So independent of the internal representation of characters as for example ASCII or EBCDIC you can get an integer digit that is represented by a character c like c - '0'.

For example in the ASCII character table the characters '0' through '9' have codes from 48 up to 57. In the EBCDIC character table they have codes from 240 up to 249.

Here is a demonstration program.

#include <stdio.h>

int main( void )
{
    for (char c = '0'; c <= '9'; c++)
    {
        printf( "'%c': %d\n", c, c - '0' );
    }
}

The program output is

'0': 0
'1': 1
'2': 2
'3': 3
'4': 4
'5': 5
'6': 6
'7': 7
'8': 8
'9': 9
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
3

In the symbol table (such as classic ASCII), C guarantees that the symbol values from '0' to '9' are placed in a contiguous sequence. In practice it might look like:

48  '0'
49  '1'
50  '2'
...

So when you subtract '7' - '0' it is the very same thing as 55 - 48 = 7. Only now you have a real integer value and not a symbol table value.

To illustrate with an example:

#include <stdio.h>

int main(void) 
{
  char a = '0';
  char b = '7';
  printf("'%c' is %d\n", a, a);
  printf("'%c' is %d\n", b, b);
  printf("%d - %d = %d\n", b, a, b-a);
}

Output:

'0' is 48
'7' is 55
55 - 48 = 7
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 1
    Is "symbol table" a good choice of words here? It has a specific meaning for compilers, and might be a little misleading. Would "code set" be a better choice? – Jonathan Leffler Mar 01 '23 at 16:55
  • Or character set. "Symbol table" also irked me – ikegami Mar 01 '23 at 17:43
  • @JonathanLeffler And I can't say ASCII or UTF8 or some EBCDIC fan will complain about that instead... – Lundin Mar 01 '23 at 20:10
  • Yeah — it's fiddly. But even in EBCDIC, the digits are consecutive code points (240-249 IIRC). I suppose that these days, using "one of the ISO 8859 code sets or UTF-8" covers many cases, and a "such as" qualifier always works to cover a multitude of not dreadfully important details. – Jonathan Leffler Mar 01 '23 at 20:15
0

You need to do - '0' operation if and only if you need to use the char as an int Example:

 char c = '7';
 int a = c;

If you print the integer with its integel value,

 printf("%d", a);

you'd get 55. That is because the value in the memory of the int contains '7' in the memory, and it's int value will be 55 in ASCII table.

If you print the integer using char, it takes the char value and gives you 7

 printf("%c", a);

Note that, while printing the formats must be fitting, for example you can't print float value %f on an int, because float on print expects double, due to more memory being required. (Double is 8 bytes, hence int and char are 4 byte values)

The result of operation char - char will be integer in the case of the char's being numericals. So in range of 0 to 9.

So if you were to print

printf("%d", '9' - '2'); 

This will give you 7 as an an integer and not char because the result of that equation result into an int.

So, substracting basic '0' char from a char is an easy way to get an integer value in return, leaving the char being substracted in its original form.

Ahmed Can Unbay
  • 2,694
  • 2
  • 16
  • 33