Thread: K&R Ch4 Polish calculator, two questions

  1. #16
    Registered User
    Join Date
    May 2011
    Posts
    36
    Quote Originally Posted by CommonTater View Post
    Check your compiler docs... you may be on a compiler or OS version that only supports single characters for ungetch().
    This is from the Pelles C, help file...


    Note that this is an older function, now branded "not standard" by the C-99 rules...

    What compiler are you using?
    The compiler is Tiny C Compiler. Also, the ungetch() that I'm using is completely defined in the example, it's not a library function. The author explains his code as:

    When an extra char is read, it is put in the buffer. When the next char is read, if there is anything in the buffer, it is pulled first.

    Just using those two facts, and forgetting about compilers, etc... I don't see how it's possible to get more than one char in the buffer, since the process that adds another (reading) will always empty out the one that's there first.

  2. #17
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Actually Windows prebuffers up to 128 characters from the keyboard and C may double buffer, providing even more....

    The limitation --no matter how poorly documented in Tiny C-- is that ungetch() only back pushes one character.

    As Tabstop points out the standard function ungetc() will pushback more than one character depending on the state of the input buffer. If the buffer has room for 10 characters, ok... if it only has room for 2... only two will be pushed back before an error is reported.

  3. #18
    Registered User
    Join Date
    May 2011
    Posts
    36
    Quote Originally Posted by CommonTater View Post
    Actually Windows prebuffers up to 128 characters from the keyboard and C may double buffer, providing even more....

    The limitation --no matter how poorly documented in Tiny C-- is that ungetch() only back pushes one character.

    As Tabstop points out the standard function ungetc() will pushback more than one character depending on the state of the input buffer. If the buffer has room for 10 characters, ok... if it only has room for 2... only two will be pushed back before an error is reported.
    You know, I stared at the original code some more and now I think my confusion is not with getch() or ungetch(). It is with getop(). Since getop() always calls getch() before calling ungetch(), it is impossible to get more than one char into the buffer by using getop(). I guess in other usage though it would be possible to call ungetch() repeatedly which could fill its buffer up. I had thought the author created ungetch() solely for the getop() example, but I guess it is supposed to have a wider usage, which is why it has the error reporting capacity?

  4. #19
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    As you keep progressing through the book, you will see these functions get reused. Such as ungetch().
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    The limitation --no matter how poorly documented in Tiny C-- is that ungetch() only back pushes one character.

    As Tabstop points out the standard function ungetc() will pushback more than one character depending on the state of the input buffer. If the buffer has room for 10 characters, ok... if it only has room for 2... only two will be pushed back before an error is reported.
    The limitation is the Standard. The Standard only guarantees one pushback. Anything else is compiler specific.


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

  6. #21
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by The111 View Post
    You know, I stared at the original code some more and now I think my confusion is not with getch() or ungetch(). It is with getop(). Since getop() always calls getch() before calling ungetch(), it is impossible to get more than one char into the buffer by using getop(). I guess in other usage though it would be possible to call ungetch() repeatedly which could fill its buffer up. I had thought the author created ungetch() solely for the getop() example, but I guess it is supposed to have a wider usage, which is why it has the error reporting capacity?
    Nope... it's an example in a book, designed to teach certain concepts and should not be seen as anything bigger or different than that. It's only provided to help you understand C programming... Real world programming is quite something else altogether.
    Last edited by CommonTater; 08-02-2011 at 02:36 PM.

  7. #22
    Registered User
    Join Date
    May 2011
    Posts
    36
    Quote Originally Posted by CommonTater View Post
    Nope... it's an example in a book, designed to teach certain concepts and should not be seen as anything bigger or different than that. It's only provided to help you understand C programming... Real world programming is quite something else altogether.
    Ok then, back to my original logic treating it only as an example in a book... in the context of that example, where getop() is the only function that calls ungetch(), is it possible to trigger the ungetch error message "too many characters"? I don't think it is possible... but at first I thought that I must be wrong, since why would the author include an error message that cannot be produced in the context of his example? That's what led me to ask the Q here.

    But somebody else said that ungetch() is used more throughout the rest of the book, so that is probably why he included the error message, I'm guessing now.

  8. #23
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by The111 View Post
    I don't think it is possible... but at first I thought that I must be wrong, since why would the author include an error message that cannot be produced in the context of his example? That's what led me to ask the Q here.
    The best error messages are the ones you never see... Programmers often include such code as a precaution because they know there are conditions (even if very rare) where a data loss might occur or because they know the function has certain limitations that might be problematic if someone modifies the code.

    But somebody else said that ungetch() is used more throughout the rest of the book, so that is probably why he included the error message, I'm guessing now.
    Frankly, I'd be prone to write that down to "a healthy dose of caution."... Something every programmer should posess so long as they don't jump over into paranoia about it.

  9. #24
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Just by looking at the code, it does not seem that it is possible to trigger that error within this context. Yes, you will see many of the generic code dealing with input/output reused within the book; just as with any other learning text on programming.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. polish calculator
    By c_lady in forum C Programming
    Replies: 14
    Last Post: 04-09-2010, 04:04 PM
  2. reverse polish calculator
    By Tool in forum C Programming
    Replies: 1
    Last Post: 12-24-2009, 05:25 PM
  3. Inverse Polish Calculator
    By Sailors in forum C Programming
    Replies: 0
    Last Post: 07-30-2007, 04:51 PM
  4. Reverse Polish Calculator
    By BlasterStar in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2003, 11:12 PM
  5. c++ Reverse Polish Calculator Help
    By knight101 in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2001, 09:31 AM