fgets() returns NULL at EOF??

Barry Shein bzs at bu-cs.BU.EDU
Fri Aug 22 15:30:09 AEST 1986


fgets() returns a char * to the string read. Traditionally, any
function that cannot return a promised pointer (such as when an EOF
occurs) returns NULL (there exists a few syscalls which return ((char
*) -1) or equivalent, c'est la vie, this has been hashed out, I guess
the rule that syscalls return -1 won [eg. sbrk].)

puts() and fputs() always return the result of the last putc() done
(at least it does in 4.2/4.3bsd.) No mention of this is made in the
manual pages I have. This will be the last character of the string
unless an error occurred, in which case it will be EOF. Notice that
for puts() this will always be '\n' (or EOF on error.)

You're intuitions seem right, they should probably both return a
similar thing (ie. fputs() should probably return a pointer to the
string printed, or NULL on error, or maybe fgets() should return the
value of the last 'getc()' done, I like the former better.) Oh well,
such is history.

Of course, in the case of getc() et al, EOF makes sense as an
out-of-band character (ie. a value that cannot be a legal char,
hence distinguishable, but no relation to a pointer.)

Thus, fgets() is consistent with the idea of returning a NULL as
a failed pointer while fputs() is consistent with the documentation
in that the doc seems to promise nothing...

	-Barry Shein, Boston University



More information about the Comp.lang.c mailing list