473,394 Members | 1,878 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

snwprintf - standard

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 ?
Nov 13 '05 #1
1 10102
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
Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Magnus Lie Hetland | last post by:
Is there any interest in a (hypothetical) standard graph API (with 'graph' meaning a network, consisting of nodes and edges)? Yes, we have the standard ways of implementing graphs through (e.g.)...
52
by: piaseckiac | last post by:
I am producing a website on air and need a link to change the entire website from standard to metric for temperature, pressure, miles-kilometers, and volume. Thank you.
6
by: John Bentley | last post by:
John Bentley writes at this level: If we think about our savings accounts then division never comes in (as far as I can see). We deposit and withdraw exact amounts most of the time. Occasionaly...
43
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
1
by: Googler | last post by:
Sorry about my mistake in subject. I was asking about swprintf not snwprintf. Anyway one more question. int swprintf(wchar_t * restrict s,size_t n,const wchar_t * restrict ormat, ...); The...
52
by: lovecreatesbeauty | last post by:
Why the C standard committee doesn't provide a standard implementation including the C compiler and library when the language standard document is published? C works on the abstract model of low...
132
by: Frederick Gotham | last post by:
If we look at a programming language such as C++: When an updated Standard comes out, everyone adopts it and abandons the previous one. It seems though that things aren't so clear-cut in the C...
1
by: manish deshpande | last post by:
Hi, When i'm installing MySQL-server-standard-5.0.24a-0.rhel3.i386.rpm by the following command: rpm -i MySQL-server-standard-5.0.24a-0.rhel3.i386.rpm the following error is being shown: ...
270
by: jacob navia | last post by:
In my "Happy Christmas" message, I proposed a function to read a file into a RAM buffer and return that buffer or NULL if the file doesn't exist or some other error is found. It is interesting...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.