2
#include <stdio.h>
main()
{
    int c;
    c = getchar();
    while(c != EOF)
        {
        putchar(c);
        c = getchar();
        }
    return 0;
}

How does copying input to output program work ? According to me at starting when first getchar() execute it return a character value and store it in c variable. Now when loop is execute compiler checks that c is equal to EOF or not, if its find that it is not then loop start working and read next statement now it execute putchar() "at that time Why compiler not print value which is store in c variable instead First compiler read all the characters then prints those characters" why ?

ecatmur
  • 152,476
  • 27
  • 293
  • 366
Vikas Verma
  • 3,626
  • 6
  • 27
  • 40
  • _Why compiler not print value which is store in c variable instead First_ it **does**... that's what `putchar(c);` does. – mah Oct 14 '13 at 11:14
  • its not answer, i want how putchar(c) does this?? – Vikas Verma Oct 14 '13 at 11:19
  • Your question was not quite clear but I believe I understand now and will provide response as a proper answer below rather than as a comment here. – mah Oct 14 '13 at 11:20
  • Guess: The output is buffered. Add `fflush(stdout);` after the putchar. – dornhege Oct 14 '13 at 11:24

1 Answers1

2

If I understand you correctly, you're saying that while the program runs you are not seeing the result of individual putchar(c); calls as they execute. Rather, you're seeing the output lumped together. You state you see it all only "at the end", but I suspect you actually see it all with each line at the time the carriage return is pressed.

Normal standard output to a terminal buffers up the characters to be written. That is, when you output something, that output doesn't get delivered to the system for actual output until some point that the buffer is flushed. In this situation, a carriage return (end of line) will flush the buffer; attempting to read input from the same channel will flush the buffer, and closing the channel (exiting the program) will flush the buffer. The purpose of this is to improve performance, since printing just a single character is very slow, while printing an entire buffer is nearly the same speed (performed once, rather than once per character).

If you want to manually flush the standard output buffer, you could include fflush(stdout); after your putchar call.

Alternatively, you could disable buffering completely via setvbuf(stdout, NULL, _IONBF, 0);

Edit: As @JonatanGoebel points out, you also have a buffering situation on the getchar(); call and the input channel. You can't just fflush that channel, but you can disable buffering on it. If it remains buffered, the input will not come to your program until a carriage return is in it (and at that point, each successive getchar() will return the next character in the buffer until you exhaust it and the call then must wait for the next buffer to fill).

mah
  • 39,056
  • 9
  • 76
  • 93
  • 1
    flushing will not solve the buffered read, only the write. So you also need to configure the stdin buffer. See this question: http://stackoverflow.com/questions/4327942/non-buffering-stdin-reading – Jonatan Goebel Oct 14 '13 at 11:30