Thread: Confused about getchar and looping

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    12

    Confused about getchar and looping

    I have the below that works but I'm not sure why it works. I understand the second loop. What I don't get is the first loop. Why would tab and space ( what the number represents in char ) be required for i? Would a while loop also work? I don't understand how the first loop is actually looping properly.

    Code:
    uint32_t j;
        char i;
        char * charArray;
    
    
    
          for ( i = getchar(); i == 9 || i == 10 || i == 32; i = getchar() )
            ;
          
          for ( j = 0; j < size - 1 && i != 10; ++j )
          {
            charArray[j] = i;
            i = getchar();
          }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You can easily see for yourself what that first loop does. Just put the code into a program that prints out the array afterwards, run it, and enter a few spaces followed by some letters. See what the output is. Then comment out that first loop, run the program, and enter a few spaces followed by some letters. See what the output is. Compare the output for each version of the program.

    It would be more clear and portable to use character literals instead of numbers; e.g.

    Code:
    for ( i = getchar(); i == '\t' || i == '\n' || i == ' '; i = getchar() )
       ;
    This would also work with a "while()" loop. In fact, that's probably the route I would take - along with using "isspace()" from <ctype.h>.

    A few other notes:
    - "getchar()" actually returns an int (for the reason why, see here: FAQ > Definition of EOF and how to use it effectively - Cprogramming.com)
    - "charArray" is not really an array, but a pointer - you would have to allocate memory to use it in this fashion

  3. #3
    Registered User
    Join Date
    Apr 2014
    Posts
    12
    Thank you! I'll mess around with it. I tried setting it up in a while loop but couldn't get it to work. Mind showing me how you would have done it?

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Why don't you show us your attempt to do this with a "while()" loop and we'll see what needs fixing.

  5. #5
    Registered User
    Join Date
    Apr 2014
    Posts
    12
    Ok I'll do that when I get to work. Thanks! And yeah, I'm not used to the pointer stuff which is why I labeled it as an array

  6. #6
    Registered User
    Join Date
    Apr 2014
    Posts
    12
    Sorry I was pretty busy at work today so only getting a chance to do it now. I guess I'm not too sure how to set up in a while loop. I tried the below and it gets stuck

    Code:
    while ( c = getchar() != '\t' && c != '\n' && c != ' ' )
          {
            c = getchar();
          }
          
          for ( i = 0; i < REG_A1 - 1 && c != '\n'; ++i )
          {
            charPointer[i] = c;
            c = getchar();
          }
    I also removed \t and \' ' - neither seem to make a difference included or not
    Last edited by andy_d; 04-30-2014 at 05:38 PM.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Take a look at an operator precedence chart: C Operator Precedence Table. Notice that relational operators (== != < <= > >=) are higher precedence than assignment (=). So what is happening is effectively this:
    Code:
    while (c = (getchar() != '\t' && c != '\n' && c != ' '))
    You are assigning the result of "getchar() is not tab AND c is not newline AND c is not space", to the variable c. That true/false value will be used to determine whether the loop continues. Note too, that c does not have an initial value yet, when it is compared to newline/space, and working with uninitialized variables is bad news. What you want is
    Code:
    while ((c = getchar()) != '\t' && c != '\n' && c != ' ')
    Those parentheses around (c = getchar()) ensure that c is assigned the char read from getchar(). That value in c is then compared to tab, newline and space. If it is neither tab, newline or space, the loop continues.

  8. #8
    Registered User
    Join Date
    Apr 2014
    Posts
    12
    Yeah I actually tried that way too but it stalls as well. Would you just put c = getchar() in the brackets?

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by andy_d View Post
    Yeah I actually tried that way too but it stalls as well.
    Please provide the smallest, compilable program that demonstrates this stalling problem. For all we know, you're doing something else before/after that causes the problem. Also, what do you mean by "stalls"? Where does it stall? Do you see any output? What input do you give it? Tell us exactly -- be specific, like "I press the space bar 3 times, then tab once, then type 'abcde' (without quotes), then hit enter".
    Quote Originally Posted by andy_d View Post
    Would you just put c = getchar() in the brackets?
    Please clarify. They are already in their own set of brackets (parentheses) as well as in a set for the entire loop condition. Did you mean your loop would only contain c = getchar()? Like so?
    Code:
    while (c = getchar())
    That would definitely be wrong.

    Also, compare your loop condition carefully to what you were checking in your first post and what Matticus had you checking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused with -> while(getchar() != newline);
    By Salahuddin in forum C Programming
    Replies: 13
    Last Post: 08-17-2011, 09:45 AM
  2. Why two getchar() ?
    By ekosix in forum C Programming
    Replies: 2
    Last Post: 06-05-2010, 01:13 PM
  3. getchar()
    By richfatsanta in forum C Programming
    Replies: 7
    Last Post: 01-20-2007, 05:55 PM
  4. Key Value - getchar();
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 12-30-2001, 10:53 PM
  5. Help With getchar();
    By User in forum C++ Programming
    Replies: 9
    Last Post: 12-14-2001, 05:54 PM

Tags for this Thread