[comp.lang.c] ungetc

edw@IUS1.CS.CMU.EDU (Eddie Wyatt) (10/19/87)

  What type of mire might I be falling into if I do this with getc and ungetc?

    char c;

    c = getc(ifile);
    ungetc(c,ifile);

    BTW  I'm not worried about c = EOF.
-- 

					Eddie Wyatt

e-mail: edw@ius1.cs.cmu.edu

gwyn@brl-smoke.ARPA (Doug Gwyn ) (10/26/87)

In article <203@PT.CS.CMU.EDU> edw@IUS1.CS.CMU.EDU (Eddie Wyatt) writes:
>  What type of mire might I be falling into if I do this with getc and ungetc?
>    char c;
>    c = getc(ifile);
>    ungetc(c,ifile);
>    BTW  I'm not worried about c = EOF.

The most likely source of trouble would be in feeding a (char)
to ungetc(), which wants an (int).  Promotion of the (char) to
an (int) may produce an unpleasant surprise in some environments,
while in others it would be no problem at all.  There shouldn't
be any concern with the ungotten character, other than to realize
that it changes the apparent (external) behavior of some input
streams such as ttys.  (If you had ungotten a character other
than the last one gotten on the same stream, then interesting
things can occur.)

mccaugh@uiucdcsb.UUCP (10/28/87)

 Not sure why you want an int for first argument to 'ungetc' -- Harbison&Steele
 report char, not int (page 316). Also, the stream must have been opened for
 input and buffered, and one character must already have been read to be pushed
 back. Perhaps you were concerned about the return-value, which is (int) c. Al-
 so, trying to push EOF has no effect and just returns EOF as value.

 mccaugh @ uiucmsl

gwyn@brl-smoke.ARPA (Doug Gwyn ) (10/30/87)

In article <165600020@uiucdcsb> mccaugh@uiucdcsb.cs.uiuc.edu writes:
> Not sure why you want an int for first argument to 'ungetc' -- Harbison&Steele
> report char, not int (page 316). Also, the stream must have been opened for
> input and buffered, and one character must already have been read to be pushed
> back. Perhaps you were concerned about the return-value, which is (int) c. Al-
> so, trying to push EOF has no effect and just returns EOF as value.

Excuse me, but good as it is, H&S is not the C language/library definition,
nor has it been generally considered as one.  When I state (for example)
that ungetc() takes an int as its first argument, you can bet that I'm
quoting from the draft proposed American National Standard for C, which
is expected to also become an ISO standard.  A former "standard" is K&R
(Kernighan & Ritchie: The C Programming Language), which is quite a bit
out of date at this point.  (I believe it was the first book about C.)

The proposed official ungetc() definition takes an int first argument,
converts it to an unsigned char, and pushes the result back onto the
input stream specified by the second argument.  It returns an int value
containing the character pushed back (after conversion) if successful,
otherwise EOF.  The stream need not be buffered, and no characters need
have been read before the pushback.  Pushback is discarded under certain
specified circumstances (e.g., a call to a file positioning function).

Some aspects of the official definition are not properly reflected by
some current implementations, so it is unwise to rely too much on the
more subtle semantics of ungetc() for the time being.

karl@haddock.UUCP (10/30/87)

In article <165600020@uiucdcsb> mccaugh@uiucdcsb.cs.uiuc.edu writes:
>Not sure why you want an int for first argument to 'ungetc' -- Harbison&Steele
>report char, not int (page 316). ...  Also, trying to push EOF has no effect

If EOF is permitted as an argument (and has a different result than 0xFF),
then the argument had better not be of type char.  H&S must've goofed here.

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint