cputsfputs

Why does C puts appends a newline while fputs doesn't?


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?


Solution

  • 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.