开发者

C++: What is the default length of an int?

开发者 https://www.devze.com 2023-01-14 22:13 出处:网络
I\'ve been searching for a while but c开发者_开发百科ouldn\'t find a definite answer to this apparently simple question: what is the default length of an int?

I've been searching for a while but c开发者_开发百科ouldn't find a definite answer to this apparently simple question: what is the default length of an int?

I know that by default, an int is signed. But is it short or long?

According to the "Fundamental data types"table found in the following page, an int is a long int by default (4 bytes). http://www.cplusplus.com/doc/tutorial/variables/

Is it always true, or does this depend on the OS (32bit/64bit), the compiler or other things?


It depends on the compiler implementor. An int is supposed to be the best "native" length for the platform. Best native here typically refers to whichever size is most handy/efficient/fast for the targeted processor to work with. Often you can expect int to have the same size as the processor's (integer) registers.

As others have pointed out, there are certain relationships about the various integer types' sizes that the compiler must adhere to, so it's the implementor is not free to choose anything. For instance, int can't be larger than long, and so on.

You often talk about programming models in relationship with issues like these, e.g. a compiler can chose to make the various types different sizes depending on the chosen model.


The standard requires only:

  • a range of a least ±32767 (i.e., at least 16 bits)
  • int is no shorter than short and no longer than long. It may be equal in size to one of them, or neither.

The exact size of integer types depends on the compiler. The de facto standard is

  • char is 8 bits
  • short is 16 bits
  • int is 16 bits on 16-bit systems, and 32 bits on both 32- and 64-bit systems
  • long may be either 32 or 64 bits


It depends on the architecture, that is the microprocessor/microcontroller you're compiling the code for (x86, ARM, PIC, Z80, 8051 etc.) and on the compiler, that is how the compiler implements the fundamental/built in data types.


You are guaranteed that a short int is at least 16 bits, and that a long int is at least 32 bits, and that plain int will be no smaller than a short nor larger than a long. But the actual sizes will be decided by the compiler implementor.


The C++ Standard says it like this :

3.9.1, §2 :

There are five 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 (44); the other signed integer types are provided to meet special needs.

(44) that is, large enough to contain any value in the range of INT_MIN and INT_MAX, as defined in the header <climits>.

The conclusion : it depends on which architecture you're working on. Any other assumption is false.


$4.4 from "The C++ programming Language" by Bjarne

Like char, each integer type comes in three forms: ‘‘plain’’ int , signed int, and unsigned int . In addition, integers come in three sizes: short int , ‘‘plain’’ int , and long int. A long int can be referred to as plain long . Similarly, short is a synonym for short int , unsigned for unsigned int, and signed for signed int .

The unsigned integer types are ideal for uses that treat storage as a bit array. Using an unsigned instead of an int to gain one more bit to represent positive integers is almost never a good idea. Attempts to ensure that some values are positive by declaring variables unsigned will typically be defeated by the implicit conversion rules (§C.6.1, §C.6.2.1). Unlike plain chars, plain ints are always signed. The signed int types are simply more explicit synonyms for their plain int counterparts.

Section 4.6 of the same book states

Sizes of C++ objects are expressed in terms of multiples of the size of a char , so by definition the size of a char is 1 . The size of an object or type can be obtained using the sizeof operator

(§6.2). This is what is guaranteed about sizes of fundamental types:

1 <= sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)

1 <= sizeof(bool) <= sizeof(long)

sizeof(char) <= sizeof(wchar_t) <= sizeof(long)

sizeof(float) <= sizeof(double) <= sizeof(long double)

sizeof(N) <= sizeof(signed N) <= sizeof(unsigned N)

where N can be char , short int, int , or long int . In addition, it is guaranteed that a char has at least 8 bits, a short at least 16 bits, and a long at least 32 bits. A char can hold a character of the machine’s character set.

This clearly indicates that sizeof(int) is implementation defined but is guaranteed to be minimum 32bits

C++03 $3.9.1/3

"For each of the signed integer types, there exists a corresponding (but different) unsigned integer type: “unsigned char”, “unsigned short int”, “unsigned int”, and “unsigned long int,” each of which occupies the same amount of storage and has the same alignment requirements (3.9) as the corresponding signed integer type40) ; that is, each signed integer type has the same object representation as its corresponding unsigned integer type.

0

精彩评论

暂无评论...
验证码 换一张
取 消