ProgrammingHardHCL · Tech Mahindra pattern
Assume 32-bit int. What is printed?
Assume 32-bit int. What is printed?
int x = 5, y = 3;
int result = x++ - ++y;
printf("%d %d %d", x, y, result);- A. 6 4 1
- B. 5 3 2
- C. 6 4 2
- D. 5 4 1
Correct answer: A. 6 4 1
x++ yields x's current value, 5, for the subtraction, then increments x to 6. ++y increments y to 4 first, then yields that new value, 4, for the subtraction. result = 5 - 4 = 1. Since the two side effects act on DIFFERENT variables (x and y), there's no undefined behaviour here, just two independent, well-defined increments.
More C Programming questions
- What is printed? c int main(void) { int a = 1, b = 2, c = 3, d = 4; int result = (a < b) ? ((c < d) ? 1 : 2) : ((c < d) ? 3 : 4)…
- What is printed? c int main(void) { int scores[5] = {85, 92, 78, 95, 88}; int max = scores[0]; for (int i = 1; i < 5; i++) { if (scores[i]…
- Assume a 64-bit LP64 ABI. What is printed? c struct Pair { int first; int second; }; struct Pair makePair(int a, int b) { return (struct…
- What is printed? c int main(void) { int n = 6; int result = 1; for (int i = n; i > 1; i--) result = i; printf("%d", result); }
- What is printed? c int main(void) { int x = 5, y = 5; if (x = y) printf("true:%d", x); else printf("false:%d", x); }
- What is printed? c int main(void) { int a = 2, b = 3; int result = a++ b-- + --a + ++b; printf("%d %d %d", a, b, result); }