Understanding c-'0 '

I tried to check out one of the K & R functions that uses c-'0 '. To make it clear, I wrote two-line code, as shown below. My question is why it prints "1". And what โ€œnumerical valueโ€ really means in this context. Thanks!

char c = 'a'; printf("%c",c-'0'); 
0
source share
7 answers

c - '0' has only a specific meaning when c is a digit ('0', '1', ... or 9).

If c is '0' , '0' - '0' - 0 , because they are equal when c '1' , '1' - '0' is 1 , because '1' immediately follows '0' in any the character set that any implementation of C. wants to use. The same goes for '2' and other digits: '9' - '0' has a value of 9 .

And you really shouldn't print the value with the format specifier "%c" .

+8
source

A numerical value is the value of an ASCII character.

'a' is 97, '0' is 48. 97-48 = 49.

49, in turn, is the value '1' , so it is printed.

+7
source

The characters in C are nothing more than an integer value. Their value is determined in accordance with the character encoding . ASCII encoding is very well known and refers to the issue published by OP. Therefore, "0" is 48, and "a" is 97. Subtracting them, you simply get the difference between these characters in the ASCII table.

In this example (equivalent to yours):

 printf("%c",'a'-'0'); 

you get 1 with 97-48 = 49, which corresponds to the ASCII character '1'.

If you used instead (note "% d" instead of "% c"):

 printf("%d",'a'-'0'); 

Then it will print only the difference (49 in this case), and not the ASCII character associated with the difference.

+6
source

The behavior of your code is implementation dependent. It is based on character encoding.

On my Linux machine, the character a encoded in ASCII (and in UTF8) as a single byte 97 (decimal), i.e. 0x61 . Similarly, the character 0 is encoded as 48 ie 0x30 . The difference 'a' - '0' is 97 - 48 , i.e. 49 , which is the character encoding 1

On some old EBCDIC machine (for example, old IBM mainframes or new ones working in some compatibility mode or in the operating system) the encoding is different.

With UTF8 (now very often used) there are many more characters (for example, C cedilla รง for French writing) and they are usually encoded with more than one byte!

+3
source

c - '0' subtracts the character code '0' (48) from c. If c represents a digit, this will result in a numerical value corresponding to c (3 for char '3', for example).

To get this numeric value, you must use the% d format specifier in printf instead of% c.

It does not make sense to subtract "0" from "a", but perhaps it will have some kind of application for a specific task.

+2
source

Let's explain this a bit more using integers instead of characters.

 char c = 97; printf("%d", c - 48); 

This, of course, will print 49, but as we move this character to the ASCII table, we get 1.

 char c = 97; printf("%c", c-48); 

This code now prints 1 because we use the char data type when printing and convert the value 49 to the equivalent ASCII number 1.

To prove this, we can try something like this.

 char a = 'a'; char b = 97; if((a == b) && ((a-'0') == (b-48))) { printf("%s", "true"); } 

First we see that a and b are equivalent, and then we see that a-'0 'is equivalent to b-48. Since both are true, we print true.

+1
source

A character within a single quote, such as '0', is a character constant. This is a constant number depending on the set of machine characters. In the ASCII character set, โ€œaโ€ is equivalent to 97, and โ€œ0โ€ is equivalent to 48.

97-48 = 49, which is equivalent to '1' in the ASCII character set. At the end, it will print '1' in the format% c.

0
source

All Articles