c - How to tell `getchar()` to go back to the beginning of the buffer? -


i learning c "the c programming language" , have ran getchar() confuses me. apparently, when call getchar() in program, input text stored in buffer , getchar() reads characters in buffer 1 one until sees eof. possible me instruct getchar() perform process second time on same buffer? instance, second loop in following program not run me. possible "reset" getchar() after first loop?

#include <stdio.h>  int main() { int c,d;   c = d = 0;    while((c = getchar()) != eof) printf("foo");    while((d = getchar()) != eof) printf("bar"); } 

it's not getchar() buffered, terminal. once hit enter, terminal sends whole line typed at once program. program's point of view, there's no buffer, sat still ten seconds, typed whole sentence in blink.

you can verify crafting loop getchar()'s, , outputs character received : output not interleaved input, appear once you've pressed enter.

thus, if want reuse know buffer, need program read actual buffer on side via fgets(), , use buffer desired.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -