C++ Quiz19 of 173
Difficulty:
⬅️ Prev: 24Quiz ID: 25Next: 26 ➡️

According to the C++23 standard, what is the output of this program?

#include <iostream>
#include <limits>

int main() {
  int i = std::numeric_limits<int>::max();
  std::cout << ++i;
}
Hint

What does the standard have to say about signed integer overflow? Does it differ from unsigned integer overflow? Also, don't trust your compiler on this one.

Reveal answer

The program is (or may be) undefined

Explanation

Signed integer overflow is undefined behaviour according to the standard §[expr.pre]¶4:

If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined.

Most implementations will just wrap around, so if you try it out on your machine, you will probably see the same as if you had done

std::cout << std::numeric_limits<int>::min();

Relying on such undefined behaviour is however not safe. For an interesting example, see https://stackoverflow.com/questions/7682477/why-does-integer-overflow-on-x86-with-gcc-cause-an-infinite-loop.

⬅️ Prev: 24Quiz ID: 25Next: 26 ➡️