How many bytes are int variables counted?

Source: Internet
Author: User

Self-built http://topic.csdn.net/t/20010521/10/128376.html

Int variables sometimes occupy 4 bytes (in Win32) and sometimes 2 bytes (in DOS)
Is the size of the int type related to the compiler, CPU, or operating system?

The so-called 16-bit 32-bit 64-bit system is determined by the CPU, determined by the addressing of machine instructions and the number of register digits.
OS is limited by the CPU, but 16-bit OS can run under 32-bit CPU (as mentioned above, pure DoS)
Many operating systems are forward compatible, that is, they allow previous programs to run. If the compiler itself is made in the 16-bit era, the operating system will provide a 16-bit sub-environment for the compiler to use.
The length of int and void * should be the same (when 16 bits are used, the 20 bits pointer is composed of two 16 bits)
Int Is only a keyword in the language definition and only visible to compiler. complier says that it has only a few digits and has nothing to do with OS/CPU.
Sizeof is always the safest method, but sizeof is only a compilation constant, and cannot achieve binary compatibility (porting)
INT and void * are as long as they are generally stored in a single register. In fact, this is not an exact explanation, the number of int digits on a XX-bit CPU is only because the CPU has a single command to operate XX-bit data (because the register is XX digits) (there may be extended commands, I don't know, but the key is to look at the number of digits in the register), so the compiler can easily use int as XX bit. This is not necessarily true, for example, the 64-bit machine will come out in the future, it may be appropriate for the compiler to extend long to 64 bits, while int is considered 32.

Well, that's why many programs do not use int, short, long, but define int32_t, int16_t, uint32_t ,..., in the future, no matter how the CPU/compiler changes, it only needs to change its typedef
Do you think that the definition of short, unsigned short (or word) must be 16 bits, while that of long and unsigned long (DWORD) must be 32 bits? I believe in word and DWORD, because they are typedef in M $ VC. Do the rest depend on the compiler?

The reason why float exists is because of floating point numbers. The reason why double exists is because of the need to process floating point numbers with higher precision. The reason why int exists is because of integers. The reason why long exists is because it needs to process integers with larger value ranges, the reason why short exists is to save space and process integers with smaller values.

Int means that when you need to define a Circular Variable (I = 0; I <100; I ++), you don't have to worry about using long or short, do you want to add unsigned .............

The reason why the standard does not specify the number of digits of int, short, and long is to leave them to the compiler for its own decision, that is, the compiler can evolve as the hardware evolves.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.