1

It seems that printf is only supposed to flush after a newline (or some other criteria). What would be a way to view this in code? For example, I tried the following test, but it seems to flush after each statement (the getchar I am guessing causes it to flush):

int main(void)
{

    printf("Hi");
    getchar();
    printf("OK!\n");

    return 0;
}

Output:

111 PrintF |
           ^
          cursor is here on first `printf`
carl.hiass
  • 1,526
  • 1
  • 6
  • 26
  • 1
    Yes - `getchar()` (or any input operation) will flush the `stdout` buffer. – Adrian Mole Jan 26 '21 at 21:16
  • @AdrianMole ok, how could I write a sample program to 'see' an example of when `printf` would not be flushed -- for example if I removed the `getchar` I couldn't tell when one `printf` was being flushed vs the other. – carl.hiass Jan 26 '21 at 21:21
  • 1
    Actually, maybe it doesn't: https://stackoverflow.com/q/2123528/10871073 – Adrian Mole Jan 26 '21 at 21:21
  • @AdrianMole that may differ between systems – M.M Jan 26 '21 at 21:22
  • @M.M Beat you by a few seconds. :-) – Adrian Mole Jan 26 '21 at 21:22
  • @carl.hiass: If you want to "see" the buffer not being flushed, you can call `printf` without a newline in a loop, and sleep for a second once per loop iteration. Then add the line `if ( i % 5 == 0) fflush( stdout );`, which will flush the buffer in every 5th loop iteration (i.e. every 5 seconds). In order to sleep, you can use the function [`sleep`](https://man7.org/linux/man-pages/man3/sleep.3.html) on Linux. – Andreas Wenzel Jan 26 '21 at 21:30
  • @AndreasWenzel ok cool, that works. Even simpler I can just do: `printf("Before");` `sleep(2);` `printf("After\n");` and it will wait 2 seconds before printing `BeforeAfter` – carl.hiass Jan 26 '21 at 21:33
  • 1
    And then for more fun, if you're able to run from a command line (Unix/Linux shell, etc.), try running with the output redirected to a file (`prog > outputfile`). Then, the output won't even be flushed at a newline -- only when a buffer fills up (typically after 512 or 1024 or 8192 characters), or the program ends. – Steve Summit Jan 26 '21 at 21:52
  • This is not a characteristic of `printf`, but rather of the standard output stream (to which `printf` directs its output) when that is connected to an interactive device. – John Bollinger Jan 26 '21 at 22:01
  • @AndreasWenzel in another comment you criticize use of sleep as not standard. Here you recommend it. You need to be more consistent. – 0___________ Jan 26 '21 at 22:29
  • See also [`printf()` anomaly after `fork()`](https://stackoverflow.com/q/2530663/15168). – Jonathan Leffler Jan 26 '21 at 22:38
  • 1
    @0_: There is nothing wrong with specifying platform-specific functions, as long as they are clearly marked as such. What I criticized was the use of the function `sleep` in an answer without making it clear that is a platform-specific solution. However, meanwhile, the OP has edited their answer to make this clear, so I have upvoted it. – Andreas Wenzel Jan 26 '21 at 22:39
  • @JonathanLeffler was your comment/link directed towards me for reference, or one of the other commenters? – carl.hiass Jan 26 '21 at 22:42
  • It was for you, @carl.hiass, as it shows a situation where line buffering leads to confusion. It's a Unix program/scenario — if you work on Windows, it is less relevant to you. That's one of a number of reasons for not bringing it to the fore (e.g. by closing your question as a duplicate of it). – Jonathan Leffler Jan 26 '21 at 22:44
  • @JonathanLeffler ok, I see. Thanks for the link I'll check it out! – carl.hiass Jan 26 '21 at 22:44

2 Answers2

2

Here's a way to test with three different cases (on unix-like devices):

#include<stdio.h>
#include<unistd.h>

int main(void)
{

    printf("1. Before (unbuffered)");
    sleep(2);
    printf("After...\n");
    printf("------------------------------\n");

    // case 2 - newline
    printf("2. Before (newline)\n");
    sleep(2);
    printf("After...\n");
    printf("-----------------------------\n");

    // case 3 - autoflush
    printf("3. Before (manual flush)");
    fflush(stdout);
    sleep(2);
    printf("After...\n");
    printf("------------------------------\n");

}
carl.hiass
  • 1,526
  • 1
  • 6
  • 26
1
void delay(unsigned someinteger) 
{ 
    clock_t start_time = clock(); 
  
    while (clock() < start_time + someinteger); 
} 

int main(void) 
{ 
        printf("start\n");
        printf("Hello");  delay(20000);
        printf("\n");
} 

See the delay between start and Hello. (some patience at the beginning required as I had to click run button)

enter image description here

0___________
  • 60,014
  • 4
  • 34
  • 74
  • 1
    If you don't use [`CLOCKS_PER_SEC`](https://en.cppreference.com/w/c/chrono/CLOCKS_PER_SEC), then, depending on the platform, the delay you set may be too short for the user to notice that data is being withheld in the buffer. – Andreas Wenzel Jan 26 '21 at 22:08