I\'m a novice in C and I came across the code like this :
int n[10];
if(c>=\'0\' && c<=\'9\')
++n[c-\'0\']
In if
Take a look at the ASCII Table, as it probably explains it well enough by itself. The decimal representation for the character '0' is 48, '1' is 49, etc. Most, if not all compilers, follow this conversion table.
By subtracting '0' (most likely the number 48), you essentially turn the character representation of the variable c
into a number representation.
edit: As mentioned in the comments, I should point out that the number representation of '0' or '9' doesn't necessarily follow the ASCII conversion table (although I believe all common compilers do). This is a technical detail, and belongs in a discussion of the ANSI C specification, and not in an answer directed to someone learning the language.
Should Ant's happen to write int number = '0';
, and wonder why it stores the number 48, it's because his compiler follows the ASCII conversion. This is both useful and helpful for a person learning the basics of C. Show me a compiler that doesn't do this, that isn't some obscure rarity, and I'll gladly leave it at that, but I think more often than not SO values pedantry over helpful replies.
That said, it's still a good thing to never use the actual number representations. That is, always prefer writing '0' over 48. But it's nice to know why '0' most likely is represented by the number 48.
First, if
is not a loop, it is a statement. There's only going to be one pass through the code.
That means the first line can read if c is a digit
and the second line combines conversion of the ascii digit to an integer digit (and increments the element of the n array to count that digit).
This allows to use a char
as an index of an array. For example you could define a string "012345"
, probably read from an external file, and compute for each character c-'0'
, which will give the integers 0
, 1
, 2
, 3
, 4
, and 5
respectively.
This (terrible) code maps the ASCII values of the digits 0..9 to zero based indexes. '0'
is of type char
, which is a numeric type. The numeric value of '0'
is usually 48
. Subtracting 48
from a char
representing a digit will give you the "value" of the char
.
'0'
denotes a character value, which can be silently converted to integer, and the result is (usually) the ASCII value of the character '0'
(which happens to be 48). The if
condition verifies that c
is (convertible into) a character value which contains a numerical digit. This is possible because the numerical digits 0 to 9 are represented by consecutive values in the ASCII chart, from 48 to 57, respectively. If you subtract the ASCII value of '0'
(i.e. 48) from the ASCII value of any numerical digit character, you get the numerical value of that digit (from 0 to 9).
So the code above indexes into an array of counters, possibly to count the appearances of each numerical digit in some piece of text.
It's a way to convert the ascii value of something to its number. In C the ascii value of the char '0' is 48. So subtracting:
'0' - '0' = 0
'1' - '0' = 1
...
c - '0' = <whatever the decimal number of c is>
Conveniently the ASCII decimal number increments are consecutive otherwise this trick won't work. In other words c
has to be one of '0'..'9' for this to work. This explains the restriction:
if(c>='0' && c<='9')