short int Type

This page was translated by a robot.

The specification shortis a modification of the inttype . A short intstores an integer value, which nowadays is usually encoded with 16 bits. Whenever possible, an integer type with an explicit number of bits from the stdintlibrary should be used instead of this type.

Details

A short int, or , for short short, is encoded with 16 bits on today's systems. However, this sizing is not guaranteed and is provided on this page as a guide only. The type shortdenotes a short integer in the sense that the largest possible integer of this type inthas fewer digits compared to a normal one.

Today, type modifiers such as short, longor long longare used less and less, since many integer types with more precise information on the number of bits are available from the C99 standard with the stdintlibrary .

A shorttype can be modified with either signedor . unsignedDepending on the case, a stores shorteither negative as well as positive values, or explicitly only positive ones. If the modifier is omitted, it is signedassumed by the compiler by default. Whether the modifier comes before or after the type doesn't matter. Likewise, it doesn't matter where the type identifier intis, or if it's actually omitted.

short               a;
short int           b;
signed short        c;
int unsigned short  d;

Positive values ​​are stored as a binary integer . The negative values ​​of a are usually encoded signed shortusing two's complement on today's computers.

The maximum and minimum possible values ​​of a shortare defined in the limitslibrary .

If the value range of one is exceeded by an arithmetic operation short, this is referred to as an overflow . On today's processors, a signed integer suddenly flips from the positive to the negative range, and in the case of unsigned integers, the number 0(zero) suddenly follows the maximum value. This is called a wrap around . Similarly, falling below the value range is referred to as underflow and the wrap-around happens vice versa. However, there is no guarantee that this effect will occur on every system.

When specifying fixed values ​​in source code, the suffix ucan be used to denote a fixed value as unsigned. However, unlike longand long long, there is no suffix shortto mark a fixed value as . Corresponding integers permanently written in the program code always have at least the type int.

Today's compilers usually issue warnings when fixed values ​​are too big for shortyou.

To output one shortwith or read in with, the modifier is used:printfscanfh


123
short s = 123;
printf("%hd\n", s);

Arithmetic Conversion

In the case of assignments, initializations or casts to a shorttype, the compiler may perform arithmetic conversions . The conversion rules for the shorttype are described below. In the case of unsafe conversions, a compiler usually issues a warning, which can be suppressed using an explicit cast if necessary.

When converting a floating point type to a floating point type short, the decimal places are simply truncated (rounding towards zero).

123
-123
printf("%hd\n", (signed short) 123.456);
printf("%hd\n", (signed short)-123.456);

If the floating point number to be converted has no place in a short, the result is undefined. On today's systems, the maximum or minimum possible value is often assumed.

32767
-32768
65535
0
printf("%hd\n", (signed   short) 1e20);
printf("%hd\n", (signed   short)-1e20);
printf("%hu\n", (unsigned short) 1e20);
printf("%hu\n", (unsigned short)-1e20);

The result can vary depending on the system. The values ​​given here correspond to the values ​​on the author's system.

When converting an integer type to an short, the value and the sign are preserved exactly in the most important cases:





123
-123
123
-123
unsigned  char  c1 =  123;
signed    char  c2 = -123;
unsigned  int   i1 =  123;
signed    int   i2 = -123;
printf("%hu\n", (unsigned short) c1);
printf("%hd\n", (signed   short) c2);
printf("%hu\n", (unsigned short) i1);
printf("%hd\n", (signed   short) i2);

The conversion of an integer value into an short, which is not included in the value range, is not defined. However, the following usually happens on today's systems: If the value to be assigned has the same number or more bits, then all those lowest-order bits of the value to be assigned are copied bit-exactly, which shortcan be accommodated in the type. If the value to be assigned has fewer bit positions, the available bit positions are shortcopied to the least significant bit positions of the . The non-defined higher-order bit positions are filled as follows: If the type of the value to be assigned is unsigned, all non-defined bit positions are filled with the bit0copied. If the type of value to be assigned is signed, the most significant bit of the available bit positions is copied to all high-order bit positions.

Because of these rules, the following cases can lead to incorrect values: A signed number cannot be converted correctly to an unsigned type. Too large an unsigned number can result in negative values ​​when cast to a signed type. When converting (signed or unsigned) numbers with a larger number (especially with int, longand long long), the value can be falsified by truncating to fewer bits or even lead to the wrong sign.

65413
-5536
14464
-31072
printf("%hu\n", (unsigned short) -123);
printf("%hd\n", (signed   short)  60000);
printf("%hd\n", (signed   short)  80000);
printf("%hd\n", (signed   short)  100000);

A Javascript program to illustrate the effects of cutting off bits can be found at Number System Converter .