why doesn't this c programme print the first printf statement?

前端 未结 3 1104
生来不讨喜
生来不讨喜 2020-12-15 12:39
#include
#include 
int main(){
      while(1)
      {

              fprintf(stdout,\"hello-out\");
              fprintf(stderr,\"hel         


        
3条回答
  •  春和景丽
    2020-12-15 13:09

    You forgot newlines (noted \n) in your strings. Or you need to call fflush(NULL); or at least fflush(stdout); before sleep(1);

    And fprintf(stdout, ...) is the same as printf(...)

    You need to output newlines or to call fflush because (at least on Linux) the stdout FILE buffer is line-buffered. This means that the C library is buffering data, and will really output it (using the write Linux system call) when the buffer is full enough, or when you flush it either with a new line, or by calling fflush. Buffering is needed because system calls are costly (calling write for every byte to be output is really too slow). Read also the man page of setbuf

提交回复
热议问题