How to detect overflow when subtracting two signed 32 bit numbers in C?
问题 I've got two signed integers, and i'd like to subtract them. I need to know if it overflowed. int one; int two; int result = two-one; if (OVERFLOW) { printf("overflow"); } else { printf("no overflow"); } Something like that. Is there a good way to do this? 回答1: Firstly, overflow in signed calculations causes undefined behavior in C. Secondly, forgetting about UB for a second and sticking to the typical overflow behavior of a 2's complement machine: overflow is revealed by the fact that result