Googler wrote:
snwprintf(s,5,L"Hello");
^?
You mean swprintf()?
However, what is the state of the buffer 's' after the call.
Does the standard say anything about this ?
Here is what the C99 standard says:
| 7.24.2.3 The swprintf function
|
| Synopsis
|
| #include <wchar.h>
| int swprintf(wchar_t * restrict s,
| size_t n,
| const wchar_t * restrict format, ...);
|
| Description
|
| The swprintf function is equivalent to fwprintf, except that the
| arguments specifies an array of wide characters into which the generated
| output is to be written, rather than written to a stream. No more
| than n wide characters are written, including a terminating null wide
| character, which is always added (unless n is zero).
|
| Returns
|
| The swprintf function returns the number of wide characters written in
| the array, not counting the terminating null wide character,or a
| negative value if an encoding error occurred or if n or more wide
| characters were requested to be written.
This gives the impression that in your examples the function should indeed
return -1. This is very surprisingly to me. In this case the answer must
be "No, the standard does not say what s will look like". Why is that
surprisingly, well take a look at snprintf ...
Also is snprintf (the non-unicode version) standard ?
in either C90 or C9x ?
C99 says ...
| 7.19.6.5 The snprintf function
|
| Synopsis
|
| #include <stdio.h>
| int snprintf(char * restrict s, size_t n,
| const char * restrict format, ...);
|
| Description
|
| The snprintf function is equivalent to fprintf, except that the output
| is written into an array (specified by arguments) 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.
Given that for your first example the function should return 4 and s
should contain "hell\0", while for your second example the function should
return 3 and s should contain "hel\0".
So in this case it is very well defined. Why does the definition of
swprintf differ that much? I hope the regulars can figure this out ...
/Sven
--
Sven Semmler
http://www.semmlerconsulting.com/
GPG Fingerprint: 72CA E26D C2A3 1FEB 7AFC 10EA F769 A9A4 937F 5E67