arttuladhar
7/20/2015 - 5:10 AM

snprintf_demo

snprintf_demo

 #include <slack/std.h>
 #ifndef HAVE_SNPRINTF
 #include <slack/snprintf.h>
 #endif

int main(int ac, char **av)
 {
 char buf[16];
 char *str = buf;
 char *extra = NULL;
 int len;

if (!av[1])
 return EXIT_FAILURE;

if ((len = snprintf(buf, 16, "%s", av[1])) >= 16)
 if (extra = malloc((len + 1) * sizeof(char)))
 snprintf(str = extra, len + 1, "%s", av[1]);

printf("%s\n", str);

if (extra)
 free(extra);

return EXIT_SUCCESS;
 }