Frage

#include<stdio.h>

main()
{
char c;
c=getchar();
putchar(c);

c=getchar();
putchar(c);

c=getchar(); 
putchar(c);

c=getchar();
putchar(c);

c=getchar();
putchar(c);

}

My 1st question is that whenever we use getchar() its create a buffer somewhere in memory and when we press enter getchar() starts reading content from buffer and getchar() read only one character at time. So in this above program when 1st getchar() will execute it creates a buffer in memory and whole line content will store in the buffer suppose I write "vik" when I hit enter getchar() starts reading content from buffer and then 'v' will assign to variable c and then only 'v' will flush from the buffer because getchar() reads only single character at a time. Then in next statement putchar(c) prints 'v' on screen. So now "ik" are left in buffer when 2nd getchar() statement execute it will not ask again to user to writing something because unless and until buffer will not flush totally its will not ask again to user to write something rather its read content from buffer. so here "ik" are remain in butter so in 2nd getchar() statement read 'i' from the buffer and then again 'i' will flush from buffer and putchar(c) prints 'i'. Now 'k' is left in buffer when 3rd getchar() execute it read 'k' from buffer and assing to c then 'k' also flush from buffer.Now at this point buffer is totally flushed,there is no content is in buffer when 4th getchar() will execute its ask to user to write something because there is no content is in buffer so lets suppose I write "as" and hits enter. Now my main question is that when I give two content "as" and there two getchar() and putchar(c) are left to execute so why only 'a' will print on screen why not 's' also ?

And, My second question is that, if we say that when we use getchar() its create a buffer in memory so if we use fflush(stdin) its have to flush the buffer. So according to this in above program when I write fflush(stdin) after the 1st putchar(c) instead of flushing buffer its executes and print same output as above program why ?

War es hilfreich?

Lösung

.Now at this point buffer is totally flushed,there is no content is in buffer when 4th getchar() will execute its ask to user to write something because there is no content is in buffer so lets suppose I write "as" and hits enter. Now my main question is that when I give two content "as" and there two getchar() and putchar(c) are left to execute so why only 'a' will print on screen why not 's' also ?

No. There is \n in the buffer. And that's why you are getting only a on inputting as.

And, My second question is that, if we say that when we use getchar() its create a buffer in memory so if we use fflush(stdin) its have to flush the buffer. So according to this in above program when I write fflush(stdin) after the 1st putchar(c) instead of flushing buffer its executes and print same output as above program why ?

fflush(stdin) will invoke undefined behavior (by the ANSI C standard). You will get anything. You are unlucky that it prints the same output.

Andere Tipps

  1. Because the new line after vik is still in buffer. So the fourth getchar()will process the new line.

  2. fflush(stdin) is not standard C, so undefined behavior, but some compiler supports it as an extension.

Note that you should declare int c otherwise you won't handle EOF properly. And you should check for EOF as well. The prototype of getchar() is :

int getchar(void);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top