My code is extremely simple, but I figure that's best to figure out what's going on in my program. For some reason, I can't seem to get ungetc() to work properly, nor can I figure out it's purpose. Normally, ungetc() should return a character back into the stream or, at least, I think it should. For example, if I type ABC, getting three characters through getchar(), I would expect ungetc() to return one of the letters getchar() had returned. If I later call putchar(), I would expect B to be returned, since C was tossed back into the stream, but C was returned. I've tried both getchar() as well as getc() and run into the same issue.
Condensed version, I typed "ABC" into my program, expecting ungetc() to cause the putc() to return 'B', but all I get is C again. Why is this happening?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
char c;
c=getchar();
c=getchar();
c=getchar();
ungetc(c, stdout);
putchar(c);
}
ungetcis an INPUT function. Useungetc(c, stdin);. BTW, all that does is putcback into the input buffer, so the nextgetcharwill get that again. It doesn't changec.