4 ms·
That particular implementation probably returns the result of the last fputc() or equivalent that it called. puts() returns EOF (typically -1) on error, or som
by _kst_ 5y ago
That particular implementation probably returns the result of the last fputc() or equivalent that it called.
puts() returns EOF (typically -1) on error, or some unspecified non-negative value on success.
fputc() returns EOF on error or the written character, treated as an unsigned char and converted to int, on success.
Don't expect all puts() implementations to do the same thing. For example, the glibc implementation appears to return the number of characters written on success. Implementations are free to rely on implementation-defined behavior. User code that's intended to be portable cannot.
- LukeShu 5y agoThat particular implementation (NetBSD's) (which is transcribed in to the article) does something more optimized than making repeated calls to `putchar()`. But as pdw's link shows, what you suggest is exactly what the historical implementation was. So NetBSD is simply matching historical Unix.
- nwellnhof 5y agoFor what it's worth, musl's implementation returns 0 on success.