fork download
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. char buf[10];
  5. for (int k = 0; k < 6; k++) {
  6. int res = snprintf(buf, k, "%s", "foo");
  7. printf("%d: snprintf() returned %d; buf has [%s] (%d chars)\n",
  8. k, res, buf, (int)strlen(buf));
  9. }
  10. return 0;
  11. }
  12.  
Success #stdin #stdout 0s 4404KB
stdin
Standard input is empty
stdout
0: snprintf() returned 3; buf has [] (0 chars)
1: snprintf() returned 3; buf has [] (0 chars)
2: snprintf() returned 3; buf has [f] (1 chars)
3: snprintf() returned 3; buf has [fo] (2 chars)
4: snprintf() returned 3; buf has [foo] (3 chars)
5: snprintf() returned 3; buf has [foo] (3 chars)