snwprintf - standard

G

Googler

What does the standard say about snwprintf in the following cases ?


For

wchar_t s[100];

snwprintf(s,5,L"Hello");

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] = 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 ?
 
S

Sven Semmler

Googler said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top