Thanks first for your time spent here. I have a question with snprintf() when size=0, with code below:
#include <stdio.h> #include <stdlib.h> int main(int ac, char **av) { char *str; int len; len = snprintf(NULL, 0, "%s %d", *av, ac); printf("this string has length %d\n", len); if (!(str = malloc((len + 1) * sizeof(char)))) return EXIT_FAILURE; len = snprintf(str, len + 1, "%s %d", *av, ac); printf("%s %d\n", str, len); free(str); return EXIT_SUCCESS; } when I run:
momo@xue5:~/TestCode$ ./Test_snprintf The result is:
this string has length 17 ./Test_snprintf 1 17 What confuses me is in the code, the size to be written is 0, why displayed 17?
What did I miss?
Thanks~~