Thread: Dont Understand some code

  1. #1
    Registered User
    Join Date
    Sep 2011

    Question Dont Understand some code

    Hey ppls am going to study computer science in Jan but am trying to learn the
    fundamentals for C,
    int main(){
    int c, white, other, i;
    int digit[10];
    white = other = 0;
    for(i = 0; i <10; i++){
    digit[i] = 0;
    while((c = getchar())!=EOF){
        if(c >= '0' && c <= '9'){
        else if(c == ' '|| c == '\t'|| c == '\n'){
        for(i = 0; i < 10; i++){
        printf("%d  ",digit[i]);
        printf(" \n%d\n%d",white,other);
    so this code i understand what it does and how it does it but the ++digit[c-'0']
    i dont get. I know its adding one to digit array but what does c-'0' mean,
    does c equal the char 0 or is it c minuz the char 0 or what am confused.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    c is a char and can be thought of as a character, but that's just a convenience for us; it is actually stored as a number (generally in ASCII).

  3. #3
    Registered User
    Join Date
    Sep 2011
    In C, character literals such as '0', 'z' and 'L' are usually just ways to help remember what ASCII Code we're referencing. (Don't confuse this with double quotes or strings!) In this case, c-'0' is equivalent to c-48. The ASCII numbers start at 48, so just taking that away will give the actual number, not the ASCII code.

  4. #4
    Registered User
    Join Date
    Sep 2011
    ooooooo thank you i been trying to solve this without realize the ASCII code, so basiclly if the input is '1' it will be 49 - 48 = 1 so its giving the the char a numerical value
    so that the digit array can read it. i get it thank you
    am a noob at this haha ty

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i dont understand this bug, please help me :(
    By Grey Kliche in forum C++ Programming
    Replies: 12
    Last Post: 08-09-2011, 08:03 AM
  2. I dont understand a piece of this code
    By lilbo4231 in forum C Programming
    Replies: 25
    Last Post: 06-13-2011, 04:15 AM
  3. Replies: 2
    Last Post: 05-03-2011, 12:29 AM
  4. What I dont understand...
    By nomi in forum C# Programming
    Replies: 7
    Last Post: 01-20-2004, 02:00 AM
  5. dont understand VS.NET
    By Qui in forum Windows Programming
    Replies: 6
    Last Post: 10-15-2003, 07:36 PM

Tags for this Thread