为什么C会在fputs不会的情况下添加一个换行符?

The C standard provides two functions, puts and fputs, with puts(s) behaving as fputs(s, stdout) except that it additionally appends a newline:

The puts() function shall write the string pointed to by s, followed by a <newline>, to the standard output stream stdout.

What is the rationale for this difference in behavior between puts and fputs?

评论
  • 浅浅的回忆
    浅浅的回忆 回复

    The puts function specifically writes to stdout which is commonly a console. Because console output is typically line bufered, it's convenient to not have to explicitly add a newline to a string to print.

    The fputs function can write to any given FILE object, not just stdout, so by not automatically adding a newline it makes the function more flexible in the general case.