Thread: ++ndigit[c-'0'] || please explain.

  1. #1
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63

    ++ndigit[c-'0'] || please explain.

    Code:
    int c, ndigit[10];
    
    while ((c = getchar()) != EOF)
    {
            if (c >= '0' && c <= '9')
               ++ndigit[c-'0'];
    this is an excerpt from a code that counts digits and whitespaces.

    ive asked around about the c-'0' expression and theyre making c = 1. 1 = 49 in ascii. and 0 = 48 in ascii. therefore c-'0' = 1. ive got that much but what i do not understand is there are more than 10 different characters that can be entered upon input. why is "if (c >= '0' && c <= '9')" used in this program when there are more than 10 different characters that can be entered?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That check rules out anything other than digits. If it is greater than '0' and less than '9', count it, otherwise ignore it.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  2. process killer on a loop
    By Anddos in forum Windows Programming
    Replies: 8
    Last Post: 01-11-2006, 01:50 AM
  3. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  4. Massive Function Problem
    By Marc Sharp in forum C Programming
    Replies: 10
    Last Post: 11-19-2003, 08:49 PM
  5. Tic Tac Toe Help
    By aresashura in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 12:52 PM