ProgrammingHard
int n = snprintf(buf, sizeof buf, "%s", text); if (n >= (int)sizeof buf) puts("truncated"); What does the test detect?
- A. Whether the output was cut short, since n is the length the full text would need
- B. Whether buf holds a NULL pointer
- C. Whether text contains a newline
- D. Whether the buffer was allocated with malloc
Correct answer: A. Whether the output was cut short, since n is the length the full text would need
snprintf returns the length the full output would have had. A value at least as large as the buffer means truncation occurred.
More C Programming questions
- char src[] = "hey"; char dst[8]; memcpy(dst, src, strlen(src)); printf("%d", (int)strlen(dst)); What is the problem with this code?
- int arr[3] = {1, 2, 3}; int (pa)[3] = &arr; printf("%d", (pa)[2]); What is printed?
- int spiralcount = 4 5; printf("%d", spiralcount); For a 4 x 5 matrix, how many cells does a full spiral visit?
- A program climbs a staircase of 5 steps, each move going up one or two steps. How many distinct ways are there?
- The number of subsets of a 4-element set with an even number of elements is what?
- The longest common subsequence of "abc" and "ac" has length?