ProgrammingHardProduct company OA
What is printed?
What is printed?
char d[6] = "zzzzz";
strncpy(d, "abc", 2);
printf("%s", d);- A. abzzz
- B. abc
- C. abczz
- D. ab
Correct answer: A. abzzz
strncpy copies at most n = 2 characters and adds a '\0' only if the source ends within those n. Here it copies 'a' and 'b' and writes no terminator, so the old 'zzz' and the original '\0' remain. That missing terminator is the classic strncpy trap.
More C Programming questions
- Assume ASCII. What is printed? c char s[] = "xyzAB"; for (char p = s; p; p++) if (p >= 'a' && p <= 'z') p = 'a' + (p - 'a' + 3) % 26…
- What is printed? c int up(int a, int b) { return (a + b - 1) / b; } int main(void) { printf("%d %d %d", up(10, 3), up(9, 3), up(0, 3)); }
- What is printed? c int t = 0; for (unsigned i = 0; i < 16; i++) for (unsigned x = i; x; x &= x - 1) t++; printf("%d", t);
- What is printed? c void s1(int a, int b) { int t = a; a = b; b = t; } void s2(int a, int b) { int t = a; a = b; b = t; } int main(void) {…
- Why does this fail to compile in C? c void f(int n) { static int k = n; }
- Which line correctly allocates space for a copy of a string pointed to by const char s?