13

Does anyone know why there is no snwprintf function in the C standard library?

I am aware of swprintf, but that doesn't have the same semantics of a true, wchar_t version of snprintf. As far as I can tell, there is no easy way to implement an snwprintf function using [v]swprintf:

Unlike snprintf, swprintf does not return the necessary buffer size; if the supplied buffer is insufficient, it simply returns -1. This is indistinguishable from failure due to encoding errors, so I can't keep retrying with progressively larger buffers hoping that it eventually will succeed.

I suppose I could set the last element of the buffer to be non-NUL, call swprintf, and assume that truncation occurred if that element is NUL afterward. However, is that guaranteed to work? The standard does not specify what state the buffer should be in if swprintf fails. (In contrast, snprintf describes which characters are written and which are discarded.)

3
  • I think you're right that you can't assume anything about the contents of the buffer when swprintf fails. Could you use errno? I know it would work on POSIX but C by itself may not require EILSEQ for encoding errors...? Sep 12, 2010 at 21:13
  • @R..: Thanks. errno is an interesting idea. I think if I want a portable solution, though, I might have to modify an actual vswprintf implementation from FreeBSD. =(
    – jamesdlin
    Sep 13, 2010 at 18:14
  • Keep in mind, reimplementing a printf-family function is probably a bad idea, because you cannot guarantee that you duplicate all the locale-specific functionality. Sep 13, 2010 at 18:20

1 Answer 1

6

See the answer given by Larry Jones here.

Essentially, swprintf was added in C95 while snprintf was added in C99 and since many implementations already returned the number of required characters (for snprintf) and it seemed a useful thing to do, that was the behavior that was standardized. They didn't think that behavior was important enough to break backwards compatibility with swprintf by adding it (which was added without that behavior several years earlier).

4
  • 2
    I agree that they couldn't break backwards compatibility by changing swprintf, but they could have added a new snwprintf function.
    – jamesdlin
    Sep 12, 2010 at 18:00
  • Apparently they didn't feel it would be worth it to add another function just to implement this behavior. Sep 12, 2010 at 18:37
  • 2
    Actually, many historic versions of snprintf prior to standardization had the same broken "return -1" behavior as swprintf. The committee deemed it broken and specified the correct behavior instead, which broke compatibility with the old Unix (SUSv2) standard. Sep 12, 2010 at 21:11
  • 1
    Actually, POSIX too seems self-contradictory in this regard. It requires: "If n or more wide characters were requested to be written...set errno to indicate the error.", but EOVERFLOW is only documented as occurring if n>INT_MAX or if the resulting string would be longer than INT_MAX. No value of errno is specified for the case when the output does not fit in n characters... Sep 12, 2010 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.