ProgrammingModerate
char b[6]; snprintf(b, sizeof b, "%s-%d", "abcdef", 12345); int r = snprintf(NULL, 0, "%s-%d", "abcdef", 12345); printf("%s %d", b, r); What is printed?
- A. abcde 12
- B. abcdef 12
- C. abcde 5
- D. abcde 11
Correct answer: A. abcde 12
snprintf writes at most size-1 characters plus a terminator, so b holds 'abcde'. Its return value is the length the full output would have had, 'abcdef-12345' has 12 characters, whatever the truncation.
More C Programming questions
- char b[5]; / stdin holds the nine bytes: abcdefgh and a newline / int calls = 0; do { fgets(b, sizeof b, stdin); calls++; } while…
- char s[] = "a,,b,c"; int n = 0; for (char t = strtok(s, ","); t != NULL; t = strtok(NULL, ",")) n++; printf("%d", n); What is printed?
- char d[8] = "ab"; strncat(d, "cdefgh", 3); printf("%zu", strlen(d)); What is printed?
- Which statement about the value returned by strcmp("apple", "Apple") is guaranteed by the C standard?
- char s[] = "abcdef"; memmove(s + 2, s, 4); printf("%s", s); What is printed?
- char a[10] = "hi"; printf("%zu", sizeof(a) - strlen(a) - 1); What is printed?