Why doesn't fputs () add a trailing newline?

or equivalently,

Why puts()

add a new trailing line?


When outputting a string, you can use two similar functions: fputs()

and puts()

.

puts()

automatically appends a newline to the end on output, but fputs()

doesn't.

Is there some reason for this inconsistency? I mean the call printf(fmtstr, ...)

and fprintf(stdout, fmtstr, ...)

has the same behavior. Why shouldn't it be for puts(str)

and fputs(str, stdout)

?

Is this due to backward compatibility or is there a good reason why they are implemented this way?

+3


source to share


2 answers


gets()

and puts()

were to be used for a simple terminal I / O, while fgets()

and fputs()

are more general, and provide finer control: you can specify the stream, and they do not perform any implicit processing newline.



Wish it gets()

was poorly specified and paved the way for so many buffer overflows. It has been permanently removed from the latest C standard.

+2


source


puts

writes to standard output, which "everyone knows" is your line-oriented terminal, or it fputs

can write to a file that does not have this connotation. In particular, it is a more convenient method than combining strlen

and fwrite

.



The same relationship between gets

(now deprecated) and fgets

.

+1


source







All Articles