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

前端 未结 12 702
旧时难觅i
旧时难觅i 2020-12-09 17:54

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

相关标签:
12条回答
  • 2020-12-09 18:24

    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
    
    0 讨论(0)
  • 2020-12-09 18:26

    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] */
    
    0 讨论(0)
  • 2020-12-09 18:31

    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.

    0 讨论(0)
  • 2020-12-09 18:38

    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 讨论(0)
  • 2020-12-09 18:41

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

    0 讨论(0)
  • 2020-12-09 18:44

    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.

    0 讨论(0)
提交回复
热议问题