More Noobery

This is a discussion on More Noobery within the C Programming forums, part of the General Programming Boards category; Hi again all I'm working through The C Programming Language , and I'm getting stuck on some array stuff There's ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    4

    More Noobery

    Hi again all

    I'm working through The C Programming Language, and I'm getting stuck on some array stuff

    There's one line in a program that I don't get. I've highlighted it in the following:

    Code:
    #include<stdio.h>
    
    
    //counts digits, white space and others//
    
    
    main()
    {
        int c, i, nwhite, nother;
    int ndigit[10]; //ndigits is an array of 10 integers//
        
        nwhite = nother = 0;
    for (i=0; i<10;++i) // setting a value for each spot in the array// 
            ndigit[i] = 0;
        
        while ((c=getchar())!=EOF)
            if (c >= '0' && c<= '9')
               ++ndigit[c-'0']; <<<<<<HERE!!!
            else if (c == ' ' || c == '\n' || c=='\t')
                ++nwhite;
    else
                ++nother;
        
    printf("digits =");
        for (i=0; i<10; ++i)
            printf(" %d", ndigit[i]);
    printf(", white space = %d, other = %d\n",
               nwhite, nother);
    }
    The book says

    the test
    if (c>= '0' && c <= '9')
    determins whether the character in c is a digit. If it is, the numeric value of that digit is
    c - '0'
    this only works if '0', '1' ..., '9' have consecutive increaing values. Fortunately, this is true for all character sets.

    I don't get the purpose of "c - '0'" is, since '0' = 0, so you're not doing anything to c (?).
    I also don't get what it means when it is talking about consecutive increasing values. Does it mean that I can't have my array be
    s[3,2,5,4,1,7,6,5], and that is has to be s[0,1,2,3,4,5,6,7,8,9]?

    Any advice would really help, I think this is stopping me from making progress in later sections of the book.

    Thanks in advanced!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Well you could try say
    printf("The value of integer 0 is %d and the value of character 0 is %d\n", 0, '0' );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    4
    Oh, the internet lied to me. I just read somewhere that 0 and '0' are the to C. That makes much more sense, thanks!

    You actually made me realize something I hadn't seen yet. Thanks again!
    Last edited by TheUnfocusedOne; 02-11-2012 at 11:32 AM.

Popular pages Recent additions subscribe to a feed

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