sprintf(3s) query

Guy Harris guy at auspex.UUCP
Thu Dec 15 04:19:38 AEST 1988


>Funny you should mention Sun and sprintf().  I have just
>learned the hard way that while most (???) C systems
>have sprintf return the number of characters written,
>the Sun version returns the buffer address!
>Let's hear it for STANDARD I/O Libraries!

No, not most.  The Version 7 "sprintf" returned the buffer address,
although this wasn't documented.  It was changed somewhere around System
III to return the number of characters printed (although, amusingly
enough, one piece of the System III SCCS code depended on it returning
the buffer address!).

SunOS started with 4.xBSD, which was derived from UNIX/32V, which was
based (more or less) on V7, so its "sprintf" returns the buffer address
- at least in the 4BSD environment.  The "sprintf" in the System V
environment (compile with "/usr/5bin/cc" rather than "(/usr)/bin/cc")
returns the number of characters printed.

Other systems that started with 4.xBSD probably also return the buffer
address, at least in their 4.xBSD environments.

>Funny, most programmers don't even know what the printf family's
>return values are supposed to be (thus permitting Sun to
>define them as they please?).

No, Sun didn't define it that way - in effect, AT&T did, when they
shipped V7 with "sprintf" returning the buffer pointer.  Sun just
followed Berkeley who followed V7....

>Have you tested printf() and fprintf() on Sun to see what THEY return?
>On AT&T 3B2 they all return number of characters.

In fact, they return the number of characters on the Sun - in both
environments.  In 4.xBSD, and probably in V7, they return, I think, 0 on
success and EOF on error.  The intent in SunOS 2.0 was to change them
all to return the number of characters printed; unfortunately, while
this broke few, if any, programs for "printf" and "fprintf", it broke a
bunch for "sprintf", so that was backed out.



More information about the Comp.lang.c mailing list