Question 12
Back to Exercise
What is the output of the following C program?
#include <stdio.h>
#include <string.h>
int main(void) {
char d[6] = "XXXXX";
strncpy(d, "ab", 4);
printf("%s %d %d %c\n", d, d[2], d[3], d[4]);
return 0;
}
- A ab 0 0 X Correct
- B abXXX 88 88 X
- C ab 0 88 X
- D ab 0 0 0
Correct Answer: Option A
strncpy copies "ab" and then fills the rest of the first n = 4 bytes with '\0'; byte 4 is untouched. When the source is longer than n, no terminator is written at all.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!