ProgrammingVery hardProduct company OA
What is printed?
What is printed?
char buf[32], *p = buf;
for (int i = 1; i <= 4; i++) p += sprintf(p, "%d,", i * i);
p[-1] = '\0';
printf("%s %td", buf, p - buf);- A. 1,4,9 9
- B. 1,4,9,16 9
- C. 1,4,9,16 8
- D. 1,4,9,16 10
Correct answer: B. 1,4,9,16 9
sprintf returns the number of characters it wrote, so p advances past "1,", "4,", "9," and "16,": 2 + 2 + 2 + 3 = 9. p[-1] overwrites the last comma with '\0', trimming the string, but p itself stays 9 bytes in.
More C Programming questions
- How many swaps does this selection sort perform? c int a[] = {3, 1, 2, 5, 4}, sw = 0; for (int i = 0; i < 4; i++) { int m = i; for (int j…
- How many element shifts (executions of a[j + 1] = a[j]) does this insertion sort make? c int a[] = {4, 3, 2, 1}, shifts = 0; for (int i =…
- How many passes of the outer loop run? c int a[] = {2, 3, 4, 5, 1}, passes = 0; for (int sw = 1; sw; ) { sw = 0; passes++; for (int j = 0…
- What is printed? c int m[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) { int t =…
- What is printed? c int x = 5; x = x << 2 >> 1; printf("%d", x);
- What is printed? c int x = 9, y = 0; if (x = y == 0) printf("A%d", x); else printf("B%d", x);