ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
int a = 2;
int b = (a++, a + 3, a * 2);
printf("%d %d", a, b);- A. 3 6
- B. 2 10
- C. 3 10
- D. 2 6
Correct answer: A. 3 6
The comma operator evaluates each sub-expression left to right (with a sequence point after each one) and the WHOLE expression's value is just its LAST operand's value, discarding the others. a++ runs first (a becomes 3); a + 3 is computed (5) but discarded; finally a * 2 is computed using a's now-current value, 3*2=6, which becomes b. Output: 3 6.
More C Programming questions
- What is printed? c int a = 10; int b = a++ + a++; printf("%d", a);
- What is printed? c int x = 5; if (x = 10) printf("true %d", x); else printf("false %d", x);
- What is printed? c int a = 1, b = 0, c = 1; int result = a || b && c; printf("%d", result);
- What is printed? c int x = 5; int y = x++ + (x = 10); printf("%d", y);
- Assume a 64-bit LP64 ABI. What is printed? c int a = 5, b = 10, c = 15; int arr[3]; arr[0] = &c; arr[1] = &a; arr[2] = &b; int sum = 0…
- Assume a 64-bit LP64 ABI. What is printed? c int a = 100; int p = &a; int q = p; p = NULL; printf("%d %d", (p == NULL), (q));