1. Home
  2. Computer Science MCQs
  3. C Programming
  4. int x = INT_MAX; x = x + 1; what does the C standard…

Added 30 Nov -0001

int x = INT_MAX; x = x + 1; what does the C standard specify?

  1. x wraps around to INT_MIN, exactly as unsigned arithmetic does, because two's complement hardware wraps
  2. The behaviour is implementation-defined, so each compiler must document the value that x receives in this case
  3. A runtime trap is guaranteed on every conforming implementation, so the program aborts instead of wrapping
  4. Signed overflow is undefined behaviour, so the compiler may assume it never happens and optimise accordingly Correct answer

Correct answer

D. Signed overflow is undefined behaviour, so the compiler may assume it never happens and optimise accordingly

15 views

MCQSEXAM

Explanation

Signed overflow is undefined behaviour under the C standard, which is why optimisations may delete checks that presume wrapping. Unsigned arithmetic is defined to wrap modulo 2 to the power N, and that difference is a frequent exam distinction. Code needing wrap-around on signed values should compute in an unsigned type and convert back.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs