ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
int main(void) {
int a = 2, b = 3;
int result = a++ * b-- + --a + ++b;
printf("%d %d %d", a, b, result);
}- A. 7 3 9
- B. Undefined behaviour: a and b are each modified more than once in this expression via multiple increment/decrement operators without sufficient sequencing between their uses
- C. 8 3 9
- D. 8 3 10
Correct answer: B. Undefined behaviour: a and b are each modified more than once in this expression via multiple increment/decrement operators without sufficient sequencing between their uses
Both a (via a++ then --a) and b (via b-- then ++b) are modified more than once within this single expression, with no sequence points between the respective operations on each variable — this is undefined behaviour, not a single well-defined arithmetic result.
More C Programming questions
- What is printed? c int main(void) { int n = 4; int triangle = n (n + 1) / 2; printf("%d", triangle); }
- What is printed? c int main(void) { int x = 15; int y = 4; printf("%d %d %d", x / y, x % y, (x / y) y + (x % y)); }
- What is the output of this C program (assume a typical 64-bit platform: int 32 bits, long and pointers 64 bits, double 8 bytes, structs…
- What is the output of this C program (assume a typical 64-bit platform: int 32 bits, long and pointers 64 bits, double 8 bytes, structs…
- What is the output of this C program (assume a typical 64-bit platform: int 32 bits, long and pointers 64 bits, double 8 bytes, structs…
- What is the output of this C program (assume a typical 64-bit platform: int 32 bits, long and pointers 64 bits, double 8 bytes, structs…