简体   繁体   中英

Meaning of data type names in C/C++

In C and C++, the size of the build-in integer types is implementation dependent. But is there any predefined intended meaning of them such that int should represent the machine word size etc?

Historically, int was supposed to mean the most "natural" type for an integer on the machine hardware; obviously, "natural" is somewhat subjective, but in the past, it was usually pretty obvious, and there weren't that many integral types available anyway, so makeing int the same size as a long or a short was the normal course of things.

For various reasons, most 64 bit platforms make int 32 bits. One could easily argue that this isn't the most "natural" length, but there was a desire that 32 bit integers be the default, and int is clearly the default integral type. Whether it is the most natural for the architecture or not becomes secondary to whether it is the size wanted as a default.

With regards to word size: historically, this was the most natural, but in many ways, it's not clear what is meant by "word size" on a modern machine: the largest size you can do arithmetic on? the size of bus transfers to and from memory? etc. Traditionally, "word size" has been used to mean both the width of internal registers (when the machine had them), or the size of a basic bus transfer. (The 8088 was usually referred to as an 8 bit machine, although it had 32 bit registers.) I wouldn't put too much meaning in it today.

There's some wording on that, but it's not very rigid:

Objects declared as characters (char) shall be large enough to store any member of the implementation's basic character set.

There are five standard signed integer types : “signed char”, “short int”, “int”, “long int”, and “long long int”. In this list, each type provides at least as much storage as those preceding it in the list. (...) Plain ints have the natural size suggested by the architecture of the execution environment , the other signed integer types are provided to meet special needs.

No strict recommendations about float sizes either:

There are three floating point types: float, double, and long double. The type double provides at least as much precision as float, and the type long double provides at least as much precision as double. (...) The value representation of floating-point types is implementation-defined.

C, unlike Java, was designed as a platform enabler and not a stand-alone platform. Cross platform compatibility took a much lower priority than working with data-type sizes that worked optimally for the given platform. Integer types are therefore not specified by the C standard and are totally platform specific

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM