1

I'm using gcc 7.3 and g++ 7.3. GCC and G++ makes error. For example,

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

int main() {
    printf("a");
    sleep(1);
    return 0;
}

'a' prints after waiting 1 seconds but when I use printf("a\n"); it works correctly. It's same on C++. For example,

#include <iostream>
#include <unistd.h>

int main() {
    std::cout << "a";
    sleep(1);
    return 0;
}

'a' prints after waiting 1 seconds, too. However, when I use std::cout << "a" << std::endl; it works correctly. What's the problem and how to fix it?

klutt
  • 25,535
  • 14
  • 43
  • 72
  • 3
    Console output is line buffered. – llllllllll Mar 24 '18 at 08:12
  • 2
    In C, if you want the output to appear immediately, call `fflush(stdout)`. – user3386109 Mar 24 '18 at 08:14
  • 1
    Wrong title (and wrong mindset: you should question your code first, before tools and standard libraries). `gcc` and `g++` are compilers. `sleep` and `printf` are froml the C standard library. All that behave as documented. **The error is in *your* code** only (not in `gcc` or `g++` as pretended by your title). – Basile Starynkevitch Mar 24 '18 at 09:34
  • Possible duplicate of [Why does printf not flush after the call unless a newline is in the format string?](https://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin) – Steve Summit Mar 24 '18 at 10:39

2 Answers2

3

sleep() is like schedule a process manually. printf() puts the data into stdout stream not directly on monitor.

printf("a"); /* data is there in stdout , not flushed */ 
sleep(1); /* as soon as sleep(1) statement occurs your process(a.out) jumped to waiting state, so data not gets printed on screen */ 

So either you should use fflush(stdout) or use \n to clear the stdout stream.

Achal
  • 11,629
  • 2
  • 14
  • 34
1

You are seeing this behaviour because stdout will be usually line buffered when used with terminal and fully buffered when used with files, the strings will be stored in a buffer and can be flushed by entering new line or when buffer fills or when program terminates

You can also override buffer mode by using setvbuf as below

setvbuf(stdout, NULL, _IONBUF, 1024);
printf("a");

It will print a without buffering, have a look at https://www.tutorialspoint.com/c_standard_library/c_function_setvbuf.htm for using setvbuf

Also have a look at different types of buffering with streams.

Hope this helps you.

  • Interesting use of `setvbuf()`: what does the `1024` size mean for an unbuffered stream? – chqrlie Mar 24 '18 at 10:42
  • In this case it is not used, you can pass any value to match the function prototype – Vasanth Alagiriswamy Mar 24 '18 at 10:59
  • This is actually not so obvious. If you pass `SIZE_MAX` as an argument and `setvbuf()` tries to allocate this amount of memory and fails, it may return a non zero value and not make the stream unbuffered. – chqrlie Mar 24 '18 at 11:26
  • I don't think with _IONBF buffer mode `setvbuf()` will execute the allocating block of code – Vasanth Alagiriswamy Mar 24 '18 at 11:36
  • Probably not, but it depends on the C library, check this question: https://stackoverflow.com/questions/49463798/what-does-the-size-argument-of-setvbuf-mean-for-an-unbuffered-stream – chqrlie Mar 24 '18 at 11:40