Questions

Is 2147483648 a long int or long int?

Is 2147483648 a long int or long int?

The 2147483648 is an integer constant that quite doesn’t fit in 32 bits, thus it will be either long int or long long int depending on whichever is the first where it fits.

Why can’t I type 2147483648 in a 32-bit signed int?

You have the number 2147483648, which cannot fit in a 32-bit signed int of two’s complement format. Since it is a decimal number (without an U, L or similar suffix), the compiler checks its internal type table (1) for such an integer constant.

What is the meaning of -2147483648 in C++?

-2147483648is not a “number”. C++ language does not support negative literal values. -2147483648is actually an expression: a positive literal value 2147483648with unary -operator in front of it. Value 2147483648is apparently too large for the positive side of intrange on your platform.

Does 2147483647 + 1 overflows?

128 2147483647 + 1is evaluated as the sum of two intsand therefore overflows. 2147483648is too big to fit in an intand is therefore assumed by the compiler to be a long(or a long longin MSVC). It therefore does not overflow. To perform the summation as a long longuse the appropriate constant suffix, i.e.

How to negate intobject -2147483648?

If you try to negate intobject, that holds value of -2147483648, then assuming two’s complement machine, the program will exhibit undefined behavior: n = -n; // UB if n == INT_MIN and INT_MAX == 2147483647

Why is the value 2147483647 + 1 undefined in C++?

Thus in ISO C/C++ 2147483647 + 1is undefined behaviouron implementations with 32-bit int. Treating it as int(and thus wrapping the value to signed negative) follows naturally from the ISO C rules for what type the expression should have, and from normal evaluation rules for the non-overflow case.