开发者

What's the real use of using n[c-'0']?

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 loop why we have to use single quotes around 0, whats the use of it, why we can't define 0 as an integer straight away? And in the second line of code ++n[c-'0'], whats the use of using array like this, in sense why we need to subtract 0(once again why the use of single quote开发者_JAVA百科s in this context?) from c in the array index?

If i do like this n[c-'0'], the result of index operation(c-'0') will be an character or integer?

Given that can anyone say me, whats the real use of such array and what are the disadvantages well?

Thanks in advance.


In C, '0' is an integer whose value represents the digit zero as a character, not the value 0, which would be the null character. Other answers have omitted this, but it's important to note that the C language mandates that the decimal digits have consecutive values, so that if c is a digit, c-'0' is the numeric value of that digit, i.e.

'0'-'0' = 0
'1'-'0' = 1
'2'-'0' = 2
.
.
.
'9'-'0' = 9


c is (likely) a char, which also has an integer representation and in C it can be converted implicitly. '0' is the character zero, and a convenient feature of numeric characters is that they are laid out sequentially in their integer representations.

So, now that you know each character has an integer representation and that the number characters are laid out sequentially, you can convert a character to its integer representation using simple subtraction.

'0' - '0' == 0
'1' - '0' == 1
'2' - '0' == 2
/* and so on and so forth */

So if you would like to count the occurrences of digits in a string, you can use this to your advantage:

int n[10]; /* 10 digits */

n['0' - '0'] /* where we store the counts for the character 0, aka n[0] */
n['1' - '0'] /* where we store the counts for the character 1, aka n[1] */


As you may already know, computers represent characters as numbers. The C standard requires that this representation must ensure that the digits must follow each other. So if n is the code of '0' then n + 9 is the code of '9'. For ASCII, these values are 48 and 57 respectively.

The code example you posted tries to be encoding agnostic so instead of checking against 48 or 57, it uses '0' as a portable constant.


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.


'0' and '9' are of int types. Their values are 48 and 57 respectively (since 48 and 57 are ASCII values of characters '0' and '9').

So you are probably holding in n an array for counting digits.

So this expression (if you are storing digit characters on c):

++n[c-'0'];

Means:

  1. Take n's position number c-'0' (c holds a character with a digit on it)
  2. Increment that position by one.

So n will be:

n[0] = x; // count of 0 characters
n[1] = x; // count of 1 characters
n[2] = x; // count of 2 characters
n[3] = x; // count of 3 characters
n[4] = x; // count of 4 characters
n[5] = x; // count of 5 characters
n[6] = x; // count of 6 characters
n[7] = x; // count of 7 characters
n[8] = x; // count of 8 characters
n[9] = x; // count of 9 characters

For example, let's say c is equal to character 2. 2's ascii value is 50. So n[50-48] becomes n[2]. So you end up using third element of array n to store 2 character count.


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


Taken out of context, it's impossible to say why the author might have done this.

What the code does is loop over the characters '0' to '9', possibly to compare them with some user input. During the body of the loop, the characters are mapped to the integers 0..9 for the purposes of indexing the array n.

Characters in C can behave like integers when involved in arithmetic, by being converted to their ASCII integer representation. The first time through the loop, c is 0, and '0' - '0' is integer 0, regardless of what the integer value of '0' is. That is, x - x will always equal 0; the actual value of x is unimportant in this case.

Given this, and the fact that the ASCII values are sequential, incrementing from 0 to 9, you can tell that the second time through the loop when c will be '1', that '1' - '0' is integer 1, and so forth.


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


Because c is a character and not an integer.

The ASCII value of '0' is 48 so a '0' would be index 48 in the n[c] statement and the programmer wanted '0' to be index 0 because n was defined n[10], so the ASCII value is converted to its integer equivalent by subtracting the code for '0' so: '0' - '0' = 0, '1' - '0' = 1, etc. The ASCII codes for '0' to '9' are 48 to 57 so the conversion is sound.

As for why, I guess someone is counting the frequency of the digits '0' to '9' in some text.


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.


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')  


-'0' subtracts the ASCII value of zero(48) from the given single-digit integer(should only contain 1 digit) the digits from '0' to '9' are stored sequentially in the ASCII code, therefore subtracting the 0's ASCII value from the given single-digit integer will give the numeric value of integer itself. for example:- the ascii value of '4' is 52 subtracting the ascii value of '0' which is 48 from 52 will give us the numeric value 4 which is equal to the given integer, therefore we use subtraction and not an addition of +'0' and also subtraction or addition of any other value will also NOT result in the same numeric value of the given integer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜