In the C 2018 standard, 6.3.1.3 3 says conversion to a signed integer type of a value that cannot be represented in the type is implementation-defined. GCC defines the conversion to wrap modulo 2N, where N is the width of the type (the number of bits in the type, including the sign bit).
In the C implementation you are using, short int
has 16 bits, so conversion wraps modulo 216 = 65,536. So 40,000 is converted to short int
by subtracting 65,536 as many times as needed to produce a value into range (−32,768 to +32,767), which is just once: 40,000−65,536 = −25,536.
Note that the bits for 40,000 in pure binary are 1001 1100 0100 0000 (215 + 212 + 211 + 210 + 26 = 32,768 + 4,096, + 2,048 + 1,024 + 64 = 40,000). When interpreted as two’s complement, the leading bit means −32,768 instead of +32,768, so the interpretation of the same bits in two’s complement is −32,768 + 4,096, + 2,048 + 1,024 + 64 = −25,536. This and the arithmetic properties of two’s complement and binary are related to the reason for GCC’s choice to wrap modulo 2N.