ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
int i, j;
for (i = 0, j = 10; i < 3; i++, j--)
;
printf("%d %d", i, j);- A. 2 8
- B. 3 8
- C. 2 7
- D. 3 7
Correct answer: D. 3 7
The comma operator lets the for-loop init and update run two statements each. i runs 0,1,2 and the loop body (empty) executes 3 times, stopping when i becomes 3; j, decremented once per iteration, goes from 10 down to 7.
More C Programming questions
- What is printed? Assume 32-bit int, two's complement. c unsigned int a = 5, b = 9; a = a ^ b; b = a ^ b; a = a ^ b; printf("%u %u", a, b);
- What is printed? c int flag = 0, mask = 2; flag |= mask == 2; printf("%d", flag);
- What is printed? c int a = 1, b = 2, c = 3; int arr[3] = {&a, &b, &c}; int p = arr; for (int i = 0; i < 3; i++) { (p + i) = 10; }…
- What is printed? (undefined behavior aside, assume typical stack layout where locals of adjacent calls can overlap after return) c int…
- What is printed? c char arr[] = {"one", "two", "three"}; char p = arr; printf("%s %c %s", p, ((p + 1) + 1), p[2]);
- What is printed? Assume 32-bit int. c int flags = 0; flags |= (1 << 2); flags |= (1 << 4); flags &= ~(1 << 2); printf("%d", flags);