I am facing weird behaviour after apply sprintf
: list[0]
seems just gone away and result of strlen
is 0
. Then I try to apply strcpy
, strlen
meets expectation and returns 3
. So my question is why sprintf
will erase my list[0]
, how do I recover value of list[0]
if I insist to apply sprintf
? thanks in advance.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main() {
char list[5][7] = { "One", "Two", "Three", "Four", "Five" };
char item[7];
int i = 0;
for (i = 0; i < 5; i++) {
sprintf(item, "%-7s", list[i]);
//strcpy(item, list[i]);
}
printf("%d", strlen(list[0]));
}
snprintf
, which guards against overflow at the risk of truncating the output.)