.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.