Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > snwprintf - standard

Reply
Thread Tools

snwprintf - standard

 
 
Googler
Guest
Posts: n/a
 
      08-22-2003
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 ?
 
Reply With Quote
 
 
 
 
Sven Semmler
Guest
Posts: n/a
 
      08-22-2003
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
add pexpect to the standard library, standard "install" mechanism. funkyj Python 5 01-20-2006 08:35 PM
How standard is the standard library? steve.leach Python 1 04-18-2005 04:07 PM
Re: snwprintf - standard Googler C Programming 1 08-23-2003 08:13 PM
snwprintf - standard Googler C Programming 1 08-22-2003 11:25 PM
snwprintf - standard Googler C Programming 2 08-22-2003 06:52 PM



Advertisments