very beginner's question

This is a discussion on very beginner's question within the C Programming forums, part of the General Programming Boards category; Hello all, I am not new to programming on high level but very new to low level programming and having ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    35

    very beginner's question

    Hello all,

    I am not new to programming on high level but very new to low level programming and having very tough time understand K&R book example at very begining.

    Can someone please clarify why below has to be subtracting 0 from the digits in the array?


    I tried just c as subscripts and does not work. It just prints 0..........
    When I look at that array, I see no benefit by subtracting 0 from digit I find in file..(at least to my naked untrained eyes)
    Please let me know.

    thank you!!!

    Code:
    main()
    {  
        int c, i, nwhite, nother;
        int ndigit[10];
    
        nwhite = nother = 0;
        for ( i = 0; i < 10; ++i )
             ndigit[i] = 0;
    
        while ( ( c = getchar() ) != EOF )
            if ( c >= '0' && c <= '9' )
                 /***     ++ndigit[c-'0'];    ****/
                  ++ndigit[c];
            else if ( c == ' ' || c == '\n' || c == '\t' )
                 ++nwhite;
            else
                 ++nother;
    
        printf("digits =");
        for ( i = 0; i < 10; ++i ) 
            /* printf("%d =====> ",i); */
            printf("%d\n", ndigit[i] );
        printf(", white space = %d, other = %d\n",
                 nwhite, nother);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    The idea is to translate the value of the character, e.g., in ASCII, to its value as an integer, i.e., a value from 0 to 9, and thus the result can be used as an index of the array of 10 integers, since the valid indices for that array is from 0 to 9.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    It's not subtracting 0, it's subtracting '0'--that is, the character zero whose value is decidedly not zero. Note that in C, you use single quotes to create character constants, such as '\n' in your example code.

    Characters in C are nothing but numbers. The character 'A', for example, is commonly equivalent to the number 65. You can test this with something like printf("%c\n", 65), although 65 is not absolutely guaranteed to be 'A'.

    It's just convenient for us to be able to type in 'A' instead of e.g. 65, but they mean the same thing.

    So you also have the characters '0', '1', '2', ... '9'. '0' thus has a number associated with it; commonly 48. If you interpret the number 48 as a character, it prints out a zero. Again, printf("%c\n", 48). If you do: char x = '0' then you're giving x the value of 48. There is no difference between char x = 48 and char x = '0'. The second form is just more convenient when we're dealing with characters (again, 48 isn't necessarily '0', but the idea is the same no matter what value '0' has on a particular system).

    Anyway, there is a specific property of digits in C: '0' + 1 gives you '1'. That is, if '0' is 48, then '0' + 1 is 49; 49 and '1' are the same thing. So '1' + 1 is '2', or 50. And so on. What this means is that if you subtract '0' from any digit (that is, from '0' to '9'), you get the value of that digit. '2' - '0' is 2, for example. '5' - '0' is 5. The code in K&R uses this to convert digits to array subscripts.

    Their array has only 10 elements, so they can use ndigit[0] to ndigit[9]. If the user types in 5, the program stores the value '5', not 5! That is, it stores the character five, not the actual value, or number five. If '5' is 53, then ndigit['5'] is clearly out of the bounds of the array. However, '5' - '0' is 5, and ndigit[5] is fine.

    A simple program might help illustrate this:
    Code:
    #include <stdio.h>
    int main(void)
    {
      int i;
      char c = '0';
      for(i = 0; i < 10; i++)
      {
        printf("Value: %d -- Character: %c\n", c, c);
        c = c + 1;
      }
      return 0;
    }

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,027
    Also note how the book has conveniently removed the return type from main, while it really shouldn't: http://cpwiki.sourceforge.net/Implicit_main
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    oh wow... very easy and very very just CLEAR.

    thank you so much..

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    i dont think u should start with K&R but it might help u to move into next level fast but if u wanna understand good try a simple book then move to K&R.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    Hi, can you recommend me a book for such case?
    I do find K&R very tough to comprehend... but at the sametime, finding perfect book for C is very hard.
    I probably am not trying very hard and I really should.
    But I have to admit the learning curve for C is very steep.

    I have been programming Perl for quite a while now and I can high level program fairly well. It's the low level stuff I want to get into now and I have tough time going through c book.

    thank you in advance!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner's Question on QT
    By unix7777 in forum C++ Programming
    Replies: 1
    Last Post: 11-30-2008, 05:53 PM
  2. beginner's question :D
    By kingliaho in forum C Programming
    Replies: 5
    Last Post: 10-17-2008, 06:20 PM
  3. beginner's question about C
    By Roberto Llovera in forum C Programming
    Replies: 5
    Last Post: 02-26-2004, 05:14 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21