snwprintf - standard

Discussion in 'C Programming' started by Googler, Aug 22, 2003.

  1. Googler

    Googler Guest

    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 ?
    Googler, Aug 22, 2003
    #1
    1. Advertising

  2. Googler

    Sven Semmler Guest

    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
    Sven Semmler, Aug 22, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Googler

    snwprintf - standard

    Googler, Aug 22, 2003, in forum: C Programming
    Replies:
    2
    Views:
    765
    Michael B Allen
    Aug 22, 2003
  2. Googler

    snwprintf - standard

    Googler, Aug 22, 2003, in forum: C Programming
    Replies:
    1
    Views:
    444
    Michael B Allen
    Aug 23, 2003
  3. Googler

    Re: snwprintf - standard

    Googler, Aug 22, 2003, in forum: C Programming
    Replies:
    1
    Views:
    388
  4. steve.leach

    How standard is the standard library?

    steve.leach, Apr 18, 2005, in forum: Python
    Replies:
    1
    Views:
    371
    Christos TZOTZIOY Georgiou
    Apr 18, 2005
  5. funkyj
    Replies:
    5
    Views:
    1,106
    funkyj
    Jan 20, 2006
Loading...

Share This Page