Thread overview
snprintf
May 10, 2004
coder
May 10, 2004
coder
Sep 07, 2004
Jackson
May 10, 2004
Does dmc implement the c99 function snprintf? My code containing calls to snprintf won't link. However vsnprintf seems to be implemented which makes snprintf easy to write. Is this an oversight.


May 10, 2004
In article <c7ohe4$fma$1@digitaldaemon.com>, coder says...
>
>Does dmc implement the c99 function snprintf? My code containing calls to snprintf won't link. However vsnprintf seems to be implemented which makes snprintf easy to write. Is this an oversight.
>
>

Also, I don't think vsnprintf is correct according to the standard. The program below always prints -1 when it should print 13.

#include <stdio.h>
#include <stdarg.h>

int snprintf(char *str, int n, char *fmt, ...)
{
va_list a;

va_start(a, fmt);

int ret = vsnprintf(str, n, fmt, a);

va_end(a);

return ret;
}

int main()
{
char str[10];

int n = snprintf(str, 10, "Hello, World\n");

printf("%d\n",n);

return 0;
}

The standard says:
"
The vsnprintf [also 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.
"

Therefore the call to snprintf should return strlen("Hello World\n") so I'd know
to allocate an array of at least strlen("Hello World\n")+1 bytes of memory. That
allows code like this to work.

int n = snprintf((char*)0, 0, fmt, a1, a2, aetc);
char *str=malloc(n+1);
snprintf(str, n+1, fmt, a1, a2, aetc);

It solves the problem of not knowing how large sprintf's buffer should be.



September 07, 2004
If the number of charactors to written exceed count,then -1 will be returned.

So just increase count.

"coder" <coder_member@pathlink.com> дÈëÏûÏ¢ÐÂÎÅ :c7ohe4$fma$1@digitaldaemon.com...
> Does dmc implement the c99 function snprintf? My code containing calls to snprintf won't link. However vsnprintf seems to be implemented which makes snprintf easy to write. Is this an oversight.
>
>