Problem with snprintf()

Corinna Vinschen vinschen@cygnus.com
Mon Aug 7 09:53:00 GMT 2000


Jonathan Larmour wrote:
> Here's what C99 says:
> 
> -=-=-=-=-
> The snprintf function is equivalent to fprintf, except that the output is
> written into an array (specified by argument s) rather than to a stream. If
> n is zero, nothing is written, and s may be a null pointer. Otherwise,
> output characters beyond the n-1st are discarded rather than being written
> to the array, and a null character is written at the end of the characters
> actually written into the array. If copying takes place between objects
> that overlap, the behavior is undefined.
> 
> Returns
> The snprintf function returns the number of characters that would have been
> written had n been sufficiently large, not counting the terminating null
> character, or a negative value if an encoding error occurred. Thus, the
> null-terminated output has been completely written if and only if the
> returned value is nonnegative and less than n.
> -=-=-=-=-
> 
> So snprintf() should always add a terminating \0.

And snprintf() may return -1 only in case of encoding error, not
if the string exceeds `size'. So that is answered as well, IMO.

Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Developer                        mailto:cygwin@sources.redhat.com
Red Hat, Inc.
mailto:vinschen@cygnus.com


More information about the Newlib mailing list