How many bytes does an int variable occupy?

Source: Internet
Author: User

int-type variables sometimes account for 4 bytes (in Win32), sometimes 2 bytes (in Dos)
The size of this int is related to the compiler, or to the CPU or to the operating system.

The so-called 16-bit 32-bit 64-bit system is determined by the CPU, which is determined by the address of machine instruction and the number of register digits.
OS is limited by CPU, but 16-bit OS can run on 32-bit CPUs (to the so-called pure dos mentioned above)
Many OS are forward compatible, so that the previous program can also run, if the compiler itself is a 16-bit era, then the OS will provide a simulation of the 16-bit environment for the compiler to use
int and void* length should be the same (16-bit 20-bit pointer is two 16-bit dislocation added)
Must turn niu Jiao jian words, int is only a language definition inside a keyword, only to compiler visible, Complier said it is several, and os/cpu are not okay
Always use sizeof is the safest way, but sizeof is only compiled from time to time, can not be binary compatible (porting)
Say int is as long as void* just because in general they should be able to be put into a single register, in fact this is not accurate, or do not understand so well, Say in XX bit CPU on the number of int is just because this level of the CPU has the XX bit data to operate a single instruction (because the Register is XX bit) (may have extended instructions, I do not know, but the key to look at the number of registers, so that the compiler to map a convenient int as XX bit, which is not necessarily, For example, after the 64-bit machine comes out, the compiler may just extend the long to 64 bits, and int or as 32, it's fine.

Oh, this is why many programs do not int,short,long, but their own definition of a lot of int32_t, int16_t, uint32_t, ..., no matter how cpu/compiler change, it only needs to change its own typedef
Do you think that short, unsigned short (or WORD) is defined by 16-bit and long, unsigned long (DWORD) must be defined by 32-bit. Word and DWORD I believe, because they are M$VC in the TypeDef, the rest is not dependent on the compiler.

The reason for float exists because of the existence of floating-point numbers, the reason for the double exists because of the need to deal with higher precision floating-point numbers, int exists because of the existence of integers, long is the reason for the need to deal with a larger range of integers, the reason for the short existence is to save space to deal with a

The meaning of int is when you need to define a loop variable (i=0;   i<100; i++), you do not need to think about whether to use long or short, do not add unsigned .....

The criteria do not dictate the number of digits of int, short, long, and leave them to the compiler for their own decision, in order for the compiler to 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.