Signed int range confusion

后端 未结 5 853
终归单人心
终归单人心 2021-01-29 09:10

This question might be very basic but i post here only after days of googling and for my proper basic understanding of signed integers in C.

Actually some say signed int

5条回答
  •  伪装坚强ぢ
    2021-01-29 09:38

    The C standard specifies the lowest limits for integer values. As it is written in the Standard (5.2.4.2.1 Sizes of integer types )

    1. ...Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.

    For objects of type int these lowest limits are

    — minimum value for an object of type int

    INT_MIN -32767 // −(215 − 1)
    

    — maximum value for an object of type int

    INT_MAX +32767 // 215 − 1
    

    For the two's complement representation of integers the number of positive values is one less than the number of negative values. So if only tow bytes are used for representations of objects of type int then INT_MIN will be equal to -32768.

    Take into account that 32768 in magnitude is greater than the value used in the Standard. So it satisfies the Standard requirement.

    On the other habd for the representation "sign and magnitude" the limits (when 2 bytes are used) will be the same as shown in the Standard that is -32767:32767

    So the actual limits used in the implementation depend on the width of integers and their representation.

提交回复
热议问题