ProgrammingVery hardProduct company OA
What is printed?
What is printed?
char a[8] = "abcdefg";
memmove(a + 2, a, 4);
char b[6] = "XXXXX";
strncpy(b, "hi", 4);
char c[4];
strncpy(c, "wxyz", 4);
printf("%s %zu %c %.4s", a, strlen(b), b[4], c);- A. ababcdg 2 X wxyz
- B. ababafg 2 X wxyz
- C. ababcdg 5 X wxyz
- D. ababcdg 2 \0 wxy
Correct answer: A. ababcdg 2 X wxyz
memmove behaves as if it copied through a temporary, so overlapping is safe: positions 2–5 receive the ORIGINAL a, b, c, d → ababcdg (a naive forward memcpy-style loop would give ababafg… that is the trap, and calling memcpy here would be undefined). strncpy(b, "hi", 4) writes 'h', 'i' and then pads with '\0' up to 4 bytes, so strlen is 2 while b[4] is still 'X'. strncpy(c, "wxyz", 4) writes NO terminator, which is why the code prints it with %.4s.
More C Programming questions
- What is printed? c struct N { int v; struct N next; }; struct N n[5] = {{1, n + 1}, {2, n + 2}, {3, n + 3}, {4, n + 4}, {5, 0}}; struct N…
- What is printed? c int lb(const int a, int n, int x) { int lo = 0, hi = n; while (lo < hi) { int mid = lo + (hi - lo) / 2; if (a[mid] < x)…
- What is printed? c int a = 7, b = 2; double x = a / b; double y = (double)a / b; double z = (double)(a / b); int w = (int)(-3.7) +…
- What is printed? c enum { RED = 2, GREEN, BLUE = GREEN + 4, N }; int a[] = { [RED] = 5, [BLUE] = 1, 9, [GREEN] = 6, 4 }; printf("%zu %d %d…
- Assume 8-bit char, 16-bit unsigned short and 32-bit int. What is printed? c int big = 300; unsigned char uc = big; unsigned short us = -1…
- What is printed? c int slot(int i) { static int box[3]; return &box[i % 3]; } int main(void) { slot(0) = 5; slot(4) = 7; int p = slot(3)…