Menu

Easy C question

2005-04-01
2012-09-26
  • Nobody/Anonymous

    Can someone please tell me why you have to append the -'0 'to

      while ((c = getchar()) != EOF){
          if (c >= '0' && c <= '9')
              ++ndigit[c-'0'];
    

    and why you can't just write ++ndigit[c]; ?

    The full program from k&r is:(not that it matters much to my question)

    include <stdio.h>

    /count digits, whitespaces and others/
    main()
    {
    int c, i, nwhite, nother;
    int ndigit[10];

      nwhite = nother = 0;
      for (i = 0; i &lt; 10; ++i)
          ndigit[i] = 0;
    
      while ((c = getchar()) != EOF){
          if (c &gt;= '0' &amp;&amp; c &lt;= '9')
              ++ndigit[c-'0'];
          else if (c == ' ' || c == '\t' || c == '\n')
              ++nwhite;
          else
              ++nother;
       }
       printf(&quot;digits=&quot;);
       for (i = 0; i &lt; 10; ++i)
           printf(&quot;%d &quot;, ndigit[i]);
       printf(&quot;,white spaces = %d, other = %d\n&quot;, nwhite, nother);
    
       system(&quot;pause&quot;);
    

    }

     
    • qWake

      qWake - 2005-04-01

      This code:

      c = getchar()

      ...reads the ASCII value of a character. These ASCII values do not match the numeric values of the digits. For example, the ASCII value of the digit character '0' is 48, the value of character '1' is 49, and so on. In order to transform this into the numeric value, you subtract 48, which happens to be the value of '0'. After doing this, you have a zero-based index for your array instead of an index that starts at 48.

      qWake

       
    • S. Thomas Bradley

      Hi Everyone:

      Okay, I pulled out my copy of K&R p22 Section 1.6 Arrays.

      It may not be obvious but you are dealing with the numbers 0 through 9 in two ways. As integer numbers and as characters.

      When you press any key on the keyboard it generates a character code from the ASCII character set (google if you don't know what that is). When the keys '1', '2', '3', etc are pressed they do not generate the number 1, 2, 3, etc. Instead they generate an ASCII character. The 0 key generates an ASCII code number 48, the 1 key generates an ASCII code number 49... 9 key generates 57.

      In C when you are referring to a single character you surround it with ' '. So if you want to save the character a in a variable you would put ' ' around it like this: char c = 'a';

      Numbers can be characters too. So to save the character 1 in a character variable you would save it as c = '1', not c = 1. In the second case 1 is an integer and you can not save integers in any variables you have defined as characters.

      Now lets talk about this piece of code: [c-'0']. What it is doing is converting the ASCII character contained in c into an integer number 0 through 9. Since the number characters are in order from '0' to '9' and zero ('0') is the first character, then by subtracting the ASCII for zero from c, it will give you the difference. That integer number is then used as an index into the array.
      Let me explain it with an example. Say that the variable c contains the character '5', the ASCII code for '5' is 53 so the value 53 is what is stored in c not the number 5. Next, the ASCII for 0 is 48. What happens is this : [c - '0'] -> ['5' - '0'] -> [53 - 48] = [5] so the result ++ndigit[c-'0'] gives ++ndigit[5] in this case.

      Hope that I didn't make this too hard, it really isn't

      See Ya
      Butch

       
    • Nobody/Anonymous

      Thank you too, Butch. I think I have an even better understanding now :) .

       
    • Nobody/Anonymous

      Thank you, qWake; now I understand :-D !

       

Log in to post a comment.

MongoDB Logo MongoDB