swprintf declaration inconsistencies?

R

Rui Maciel

I've just noticed that the swprintf() function is declared differently in
some platforms. As far as I know, GCC declares that function as

int swprintf(wchar_t *s, size_t n, const wchar_t *format, ...);

That definition is also present in references like:
http://www.opengroup.org/pubs/online/7908799/xsh/swprintf.html

On the other hand, it appears that at least the compiler from Microsoft
Visual Studio 2003 declares that function as:

int swprintf(wchar_t *s, const wchar_t *format, ...);

So what gives? First of all, is this function a part of the C standards? If
so, what is the standard definition, i.e., what is the right one?


Thanks in advance?
Rui Maciel
 
J

jacob navia

Rui said:
I've just noticed that the swprintf() function is declared differently in
some platforms. As far as I know, GCC declares that function as

int swprintf(wchar_t *s, size_t n, const wchar_t *format, ...);

That definition is also present in references like:
http://www.opengroup.org/pubs/online/7908799/xsh/swprintf.html

On the other hand, it appears that at least the compiler from Microsoft
Visual Studio 2003 declares that function as:

int swprintf(wchar_t *s, const wchar_t *format, ...);

So what gives? First of all, is this function a part of the C standards? If
so, what is the standard definition, i.e., what is the right one?


Thanks in advance?
Rui Maciel

The standard says:

7.24.2.3 The swprintf function

Synopsis

1 #include <wchar.h>
int swprintf(wchar_t * restrict s,size_t n,
const wchar_t * restrict format, ...);

Description
2 The swprintf function is equivalent to fwprintf, except that the
argument s 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
3 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.
 
R

Robert Gamble

I've just noticed that the swprintf() function is declared differently in
some platforms. As far as I know, GCC declares that function as

int swprintf(wchar_t *s, size_t n, const wchar_t *format, ...);

That definition is also present in references like:http://www.opengroup.org/pubs/online/7908799/xsh/swprintf.html

On the other hand, it appears that at least the compiler from Microsoft
Visual Studio 2003 declares that function as:

int swprintf(wchar_t *s, const wchar_t *format, ...);

So what gives? First of all, is this function a part of the C standards? If
so, what is the standard definition, i.e., what is the right one?

swprintf was added in Amendment 1 circa 1995, the current standard
declaration is:

#include <wchar.h>
int swprintf(wchar_t * restrict s, size_t n, const wchar_t * restrict
format, ...);

The restrict qualifiers were added in C99, the rest was the same. If
I recall correctly, implementations of swprintf already existed before
it was standardized but the size parameter was added when it became
part of the standard so that it wouldn't have the same problems that
sprintf suffered from.

Robert Gamble
 
R

Rui Maciel

jacob said:
The standard says:

7.24.2.3 The swprintf function

Synopsis

1 #include <wchar.h>
int swprintf(wchar_t * restrict s,size_t n,
const wchar_t * restrict format, ...);

<snip />

Nice. Thanks for the help, Jacob and Robert, and for replying so quickly.
Kudos!


Rui Maciel
 

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

Forum statistics

Threads
474,262
Messages
2,571,059
Members
48,769
Latest member
Clifft

Latest Threads

Top