In my current understanding, you're supposed to call ungetc when you want to "un-get" the most recent character that you got from the stream, sort of like reversing the effect of an equivalent call to fgetc. If that's the case, then what's the point of having a parameter to specify which character to put back? Shouldn't the stream remember at least what its most recently obtained character was?
I was thinking that it could be just ungetc(stream), which probably makes more sense. I've seen a lot of implementations that involve the use of ungetc, and so far, none of them have really shown that the function is capable of pushing back a different character and explain how this behavior can be useful.
You may use any character in the call of
ungetc. It is not necessary that it must be the same as the last read character.That is the function has a more general behavior.
More general functions have more applications.
For example if the last read character is tab character
'\t'you can substitute it for space character' 'in the input stream.