long int
Type
This page was translated by a robot.
The specification longis a modification of the inttype . A long intstores an integer value, which nowadays is usually encoded with 32 bits. Whenever possible, an integer type with an explicit number of bits from the stdintlibrary should be used instead of this type.
Details
A long int, or , for short long, is encoded with 32 bits on today's systems. However, this sizing is not guaranteed and is provided on this page as a guide only. The type longdenotes a long integer in the sense that the largest possible integer of this type inthas more digits compared to a normal one.
In today's systems, a longtype intis equivalent and therefore often only exists for reasons of compatibility with older code or as a distinguishing feature in protocol or file format descriptions.
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 longtype can be modified with either signedor . unsignedDepending on the case, a stores longeither 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.
Positive values are stored as a binary integer . The negative values of a are usually encoded signed longusing two's complement on today's computers.
The maximum and minimum possible values of a longare defined in the limitslibrary .
If the value range of one is exceeded by an arithmetic operation long, 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 the source code, intvalues can be appended with a small lor a large Lsuffix. Both suffixes mean the same thing, but it is recommended to use the uppercase variant to avoid confusion with the digit 1(one). The suffix tells the compiler that the value is a longvalue. However, since the longtype is usually the same as the inttype in today's compilers, the suffix is generally omitted today.
987654321l
987654321L
987654321
To explicitly unsignedmark a fixed value as , the suffix ucan be used.
Compilers usually issue warnings when fixed values are too large for one intand should be marked as a longvalue . However, as nowadays the type is intthe longsame, this warning is hardly ever seen.
To print or read in the modifier long( small ) is used:printfscanflL
Arithmetic Conversion
In the case of assignments, initializations or casts to a longtype, the compiler may perform arithmetic conversions . The conversion rules for the longtype 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 long, the decimal places are simply truncated (rounding towards zero).
If the floating point number to be converted has no place in a long, the result is undefined. On today's systems, the maximum or minimum possible value is often assumed.
2147483647
-2147483648
4294967295
0
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 long, the value and the sign are preserved exactly in the most important cases:
The conversion of an integer value into an long, 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 longcan be accommodated in the type. If the value to be assigned has fewer bit positions, the available bit positions are longcopied 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 of higher magnitude (especially with long long) the value can be falsified by the truncation to fewer bits or even lead to the wrong sign.
4294967173
-294967296
1705032704
-589934592
A Javascript program to illustrate the effects of cutting off bits can be found at Number System Converter .