...

/

Discussion: Hello, stdin

Discussion: Hello, stdin

Execute the code to understand the output and gain insights into buffer management.

Run the code

Now, it's time to execute the code and observe the output. After you press the "Run" button, press "Enter" to see the output.

#include <stdio.h>

int main()
{
    char buffer[BUFSIZ]; /* BUFSIZ is a constant defined in stdio.h */
    setbuf(stdout, buffer);
    puts("Wait for it!");
    puts("Wait for it!");
    puts("Now!");
    getchar();
    puts("Whew!");
    return(0);
}
C code for the given puzzle

Understanding the output

No output is generated when the program starts. Press the "Enter" key, and the following text appears:

Wait for it!
Wait for it!
Now!
Whew!
Code output

We won’t understand the output order unless we know what the setbuf() function does. Even if you don’t, look at the presentation:

setbuf(stdout, buffer);
Set output buffer

The first argument is the stdout constant, representing the standard output device. The second argument is the character array buffer. As the ...