ProgrammingModerate
char d[6] = "abc"; /* we want strncat(d, "defgh", 5) with no overflow */ What is the smallest size d needs?
- A. 6
- B. 9
- C. 8
- D. 10
Correct answer: B. 9
strncat may append 5 characters and then needs a terminator, so 3 + 5 + 1 = 9 bytes are required. The 6-byte array overflows.
More C Programming questions
- char b[16]; sprintf(b, "%d%%", 50); printf("%zu", strlen(b)); What is printed?
- printf("%c", 321); What is printed?
- char a[20], b[20]; / stdin: "hi therennextn" / scanf("%s", a); fgets(b, sizeof b, stdin); What is stored in b?
- char w[] = "ab", z[2] = "ab"; printf("%zu", sizeof w + sizeof z); What is printed?
- For a string s of length up to 2 x 10^5 stored in a char array, which in-place reversal is correct and fast enough?
- What is the value of sizeof("hello") in C?