Entire ranges using the sizeof operator

Consider this:

1. printf("%d", sizeof(32767));
2. printf("%d", sizeof(-32767));
3. printf("%d", sizeof(-32768));

1 and 2 gave the result as 2, while the third operator gave 4, but the range intis from -32768 to 32767. Why is there a difference only in the third statement? can someone explain this please?

in my compiler size intis 2.

+3
source share
3 answers

Value is -32768not considered as a separate element of the language, it consists of two parts - sign and meaning.

The value 32768cannot be intwhen the upper limit is 32767. Thus, it is defined as a larger type, for example long. Only after this sign is applied.

+6
source

-, 32768. 32768 int ( ), long.

, , , INT_MIN (-32767-1), .

+3

int , , , (, long), ( int).

a int - 16 , ( 2 ) -32768 +32767. , abs(), ( 16-) abs(-32768). , INT_MIN -32767; abs() int (, abs(-32768) = -32768).

The end result of this is that the integer constant -32768, which could theoretically be inserted into 16-bit int, can be raised to longbecause it is smaller INT_MIN.

+1
source

All Articles