ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
int a = 1;
int b = a++ + a++;
int c = ++a + ++a;
printf("UB UB");
  1. A. Both b and c are computed with well-defined, predictable values
  2. B. b's computation is undefined behaviour, but c's is well-defined
  3. C. Both b's and c's computations are undefined behaviour: each modifies a twice with no sequence point separating the modifications
  4. D. c's computation is undefined behaviour, but b's is well-defined

Correct answer: C. Both b's and c's computations are undefined behaviour: each modifies a twice with no sequence point separating the modifications

Both a++ + a++ and ++a + ++a modify the same variable a twice within a single expression, with + providing no sequencing guarantee between evaluating its two operands. Both expressions are undefined behaviour in C, regardless of whether one 'looks safer' than the other.

More C Programming questions

Exams that ask this topic