Subject: Re: Query about return value of snprintf
To: Chris G. Demetriou <firstname.lastname@example.org>
From: Andreas Jaeger <email@example.com>
Date: 06/18/1999 00:14:45
>>>>> Chris G Demetriou writes:
Chris> firstname.lastname@example.org writes:
>> The NetBSD man page says that we do:
>> These functions return the number of characters printed (not including
>> the trailing `\0' used to end output to strings).
>> Which is what I expected.
Chris> No, that's part of what it says.
Chris> Further it says:
Chris> snprintf() and vsnprintf() will write at most size-1 of the characters
Chris> printed into the output string (the size'th character then gets the ter-
Chris> minating `\0'); if the return value is greater than or equal to the size
Chris> argument, the string was too short and some of the printed characters
Chris> were discarded.
Chris> i.e. it actually returns the number of characters that would have been
Chris> printed, given an adequately-sided buffer.
>> RETURN VALUE
>> If the output was truncated, the return value is -1, oth-
>> erwise it is the number of characters stored, not includ-
>> ing the terminating null.
Chris> I consider the BSD behaviour much preferred to what you quote as the
Chris> GNU libc behaviour.
I agree. And since ISO C9x specifies this behaviour also, glibc has
been changed (see my other email).
Andreas Jaeger email@example.com firstname.lastname@example.org
for pgp-key finger email@example.com