Difference between write() and printf()

后端 未结 6 1769
春和景丽
春和景丽 2020-12-09 04:03

Recently I am studying operating system..I just wanna know:

What’s the difference between a system call (like write()) and a standard library function (like printf()

6条回答
  •  粉色の甜心
    2020-12-09 04:47

    I am writing a small program. At the moment it just reads each line from stdin and prints it to stdout. I can add a call to write in the loop, and it would add a few characters at the end of each line. But when I use printf instead, then all the extra characters are clustered and appear all at once, instead of appearing on each line.

    It seems that using printf causes stderr to be buffered. Adding fflush(stdout); after calling printf fixes the discrepancy in output.

提交回复
热议问题