ProgrammingHard
char d[4]; char s[] = "abc"; strncpy(d, s, sizeof d - 1); d[sizeof d - 1] = '\0'; printf("%s", d); What is printed?
- A. abc
- B. ab
- C. abcd
- D. abx
Correct answer: A. abc
strncpy copies at most three characters, and the explicit terminator at the last slot makes the string complete.
More C Programming questions
- char w[16]; scanf("%15s", w); Why is the width 15 used?
- 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?