I have this in my code and it is causing the warning that follows:
long ans = ((long) INT_MIN) * 2 - 1;
The warning that I get is:
warning: integer overflow detected: op "*"
I ha开发者_开发问答ve included limits.h so that I could use INT_MIN
#include <limits.h>
This means that the calculation will overflow the range of long
. Signed overflow yields undefined behavior.
The only correlation between the range of values representable by long
and the value INT_MIN
is that INT_MIN
is representable as a long
. There is no guarantee that one less than twice INT_MIN
is representable as a long
.
On Windows x86, using the Visual C++ compiler, long
is 4-bytes, same as int
. Try long long
精彩评论