snwprintf - standard
What does the standard say about snwprintf in the following cases ?
I know the return value should indicate error. (eg. -1)
However, what is the state of the buffer 's' after the call.
Does the standard say anything about this ?
Should there be s = 0;
Should s contain 'H','e','l','l','\0' (L omitted for brevity)
Likewise for say
snwprintf(s,4,L"Hello"); /* This isn't a border case like the
earlier but an error all the same */
Or is it undefined/implementation defined ?
Also is snprintf (the non-unicode version) standard ?
in either C90 or C9x ?
Re: snwprintf - standard
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:
| 184.108.40.206 The swprintf function
| #include <wchar.h>
| int swprintf(wchar_t * restrict s,
| size_t n,
| const wchar_t * restrict format, ...);
| 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).
| 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 ...
| 220.127.116.11 The snprintf function
| #include <stdio.h>
| int snprintf(char * restrict s, size_t n,
| const char * restrict format, ...);
| 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.
| 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 Semmler http://www.semmlerconsulting.com/
GPG Fingerprint: 72CA E26D C2A3 1FEB 7AFC 10EA F769 A9A4 937F 5E67
|All times are GMT. The time now is 01:52 PM.|
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.