ProgrammingHard
char w[16]; scanf("%15s", w); Why is the width 15 used?
- A. It reads exactly 15 characters, padding with spaces
- B. It leaves room for the terminator in a 16-byte buffer, so an overlong word cannot overflow it
- C. It stops at the first digit in the input
- D. It makes scanf faster by skipping newlines
Correct answer: B. It leaves room for the terminator in a 16-byte buffer, so an overlong word cannot overflow it
A width of 15 stores at most 15 characters plus the terminator in the 16-byte array. Longer words are cut off in the input stream.
More C Programming questions
- int c = 0; const char s = "Queue"; for (int i = 0; s[i]; i++) if (strchr("aeiou", tolower((unsigned char)s[i]))) c++; printf("%d", c)…
- printf("%.s", 2, "hello"); What is printed?
- int n = snprintf(buf, sizeof buf, "%s", text); if (n >= (int)sizeof buf) puts("truncated"); What does the test detect?
- 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?