TL; DR
Behavior is valid and such compilers / architectures exist
- TI DSP with 4 byte
int , 5 byte long - Motorola DSP5600x / 3xx Series with 2 bytes
short , 3 bytes int , 6 bytes long - x86 with 8-byte
double , 10-byte long double
The number of bits used to represent the long type does not always match the number of bits of the int type or an integer. You may need to be able to represent a larger range of values (than is possible with int), but processor overhead can also be considered.
Derek M. Jones "The New Standard C (material outlined) - economic and cultural commentary
Another answer already indicates standard C ++ requirements. Similarly, the C standard also does not limit type sizes (floating point or integer) in bytes with authority 2. The most common example is a long double , which is often 10 bytes in x86 (indented to 12 or 16 bytes in many modern compilers )
ISO / IEC 9899: 1999 (E)
5.2.4.2.1 Dimensions of integer types
- The values below should be replaced with constant expressions suitable for use with
#if preprocessing directives. In addition, in addition to CHAR_BIT and MB_LEN_MAX , expressions that are of the same type as the expression that is an object of the corresponding type, converted in accordance with whole promotions, are replaced. Their values, determined by the implementation, must be equal or greater in magnitude (in absolute value) to those shown with the same sign. [...]
6.2.5 Types
There are five standard standard integer types, designated signed char , short int , int , long int and long long int . (These and other types can be assigned in several additional ways, as described in 6.7.2.) There can also be extended types with extended support that are signed with an integer type. 28)
Standard and extended signed integer types are called signed integer types. 29)
For any two integer types with the same degree of correspondence and a different integer conversion (see 6.3.1.1), the range of values of a type with a lower number of conversion rank is a sub-range of values of another type .
Odd integer types are much less common, but still exist. Many DSPs have standard compliant compilers with non-power-or-2 types , where int has 32 bits, long has 40 bits .
long- 40 bits or 5 bytes for C6000 COFF. This is fully compatible with any major C / C ++ standard, since all of these standards define a minimum requirement of 4 bytes (aka. Long int) . Programmers often mistakenly assume that this type is exactly 4 bytes in size.
- 32 bits or 4 bytes for C6000 EABI / ELF. See the C6000 EABI Migration Page for a discussion of the impact when porting code that has been encoded with 40-bit time but only sees a 32-bit migration target platform.
Focus on
C89 support in TI compilers # Misunderstanding about TI C
Offsite note: on some objects, even long long also a 32 or 40-bit type that is valid in C89 as an extension, but violates C99
Some targets have a long long (extension from C99), but not appropriate. C99 requires at least 64 bits, but the C2700 has a 32-bit long long , and the C5500 has a 40-bit long long . The C2800, C6000, and ARM have a 64-bit long long , while the C5400 and MSP430 do not support long long . This is not technically a violation of C89, since it is actually an extension, but if we start supporting C99, it will be a violation of C99 (C99 5.2.4.2.1 "Sizes of integer types", clause 1).
A wider font size does not even have to be a multiple of its previous type size
The number of bits used to represent the long type does not always match the number of bits of the int type or an integer. You may need to be able to represent a larger range of values (than is possible in an int type), but processor overhead can also be considered. For example, Texas Instruments TMS320C6000, a DSP processor, uses 32 bits to represent int and 40 bits to represent long (this choice is not uncommon). These processors (usually DSPs), which use 24 bits to represent int , often use 48 bits to represent long . The use of representations of the 24/48 bit integer type may be due to application requirements when the 32/64 bit integer type representation is not cost-effective.
Derek M. Jones "New Standard C (excerpts from material): economic and cultural commentary
In all 24-bit DSPs that I knew before, CHAR_BIT == 24 , and all types are 24 bits wide , but I just found out that the Motorola DSP5600x / 3xx series has a really "weird" type system
Data Type size in bits (un)signed char 8 (un)signed short 16 (un)signed int 24 (un)signed long 48 (long)_fract 24 (48) pointer 16/24 float/double 24+8 enum 24
So, in this case, sizeof(char) == 1 && sizeof(short) == 2 , but sizeof(int) == 3 and sizeof(long) == 6
Unfortunately, GCC calls it ( long or long long ) double-word integers , and so most people do, making a big misunderstanding, although it doesn't have to be twice as large.