tags:

views:

456

answers:

3

Can anyone explain to me the purpose of ungetch? This is from K&R chapter 4 where you create a Reverse Polish Calculator.

I've ran the program without the call to ungetch and in my tests it still works the same.

 int getch(void) /* get a (possibly pushed back) character */
    {
        if (bufp > 0)
        {
         return buf[--bufp];
        }
        else
        {
         return getchar();
        }
    }

    void ungetch(int c) /* push character back on input */
    {
        if (bufp >= BUFSIZE)
        {
         printf("ungetch: too many characters\n");
        }
        else
        {
         buf[bufp++] = c;
        }

}

(I've removed the ternary operator in getch to make it clearer.)

Thanks!

+4  A: 

Try running the program without spaces around operators. I don't recall precisely the format of that example and I don't have K&R handy, but instead of using "2 3 +" try "2 3+". The ungetch() is probably used when parsing numbers, as the number parser will read digits until it gets something that is a non-digit. If the non-digit is a space, then the next getch() will read the + and all is well. However, if the next non-digit is a +, then it will need to push that back onto the input stream so the main read loop can find it again.

Hope I'm remembering the example correctly.

Greg Hewgill
Thanks that does the trick!
Tyler
Great! Since you're new to Stack Overflow, don't forget to "accept" (click the check mark) on the most helpful answer.
Greg Hewgill
+8  A: 

I don't know about the specific example you're referring to (It's probaby 23 years since I read K&R, and that was the first edition.), but often when parsing it's convenient to 'peek' at the next character to see if it is part of what you're currently parsing. For instance, if you're reading a number you want to keep reading digits until you come to a non-digit. Ungetc lets the number reader look at the next character without consuming it so that someone else can read it. In Greg Hewgill's example of "2 3+", the number reader would read the 3 digit, then read the plus sign and know the number is finished, then ungetc the plus sign so that it can be read later.

David Norman
Thanks a lot.It makes a lot more sense now.
Tyler
Though it might be just semantics, ungetc allows you to "push back" a character after reading it so that it's re-read by the next read call. That's not the same as peeking, which implies a read without consumption.
Software Monkey
Yes, peek is equivalent to getc followed by ungetc.
David Norman
+2  A: 

It's used a lot for lexical scanners (the part of the compiler that breaks your text into chunks like variable names, constants, operators, etc.). The function isn't necessary for the scanner, it's just very convenient.

When you're reading a variable name, for example, you don't know when you're done until you read a character that can't be part of the variable name. But then you have to remember that character and find a way to communicate it to the next chunk of the lexer. You could create a global variable or something, or pass it to the caller--but then how do you return other things, like error codes? Instead, you ungetch() the character to put it back into the input stream, do whatever you need to with your variable name and return. Then when the lexer starts reading the next chunk, it doesn't have to look around for extra characters lying around.

Thanks for the reply.It's helpful to have real-life examples of usage.
Tyler