Size of integer in C [duplicate]

2020-07-18 09:43发布

问题:

This question already has answers here:
Closed 8 years ago.

Possible Duplicate:
Does the size of an int depend on the compiler and/or processor?

Does the size of Integer depend on Compiler or on OS or on Processor? What if I use gcc on both 32 bit OS or 64bit OS running either on 32 bit machine or 64 bit machine(only 64 bit OS in this case).

回答1:

Depends on compiler options.
Of course it depends on the compiler itself too.
But the compiler was made for a specific OS, so it depends on the OS
And / or
The compiler was made for a specific processor, so it depends on the processor



回答2:

It depends on the combination of compiler, processor and OS.

For instance, on a 64 bit Intel CPU, in 64 bit mode, the size of a long int in Windows is 4 byte while in Linux and on the Mac it is 8 byte. int is 4 bytes in all three OSes on Intel.

The compiler implementer also has a choice, but usually uses what the OS uses. But it could well be that a compiler vendor that has C compilers for all three platforms decides to use the same sizes in all three.

Of course, it doesn't make sense to make int 4 bytes (although it would be possible) on a 16 bit CPU.

So it depends on all three things you mention.



回答3:

The size of int, long, etc, depends on the compiler, but the compiler implementer will choose the best size for a particular processor and/or OS.



回答4:

It depends on the system. And by system I mean any combination of processor and operating system, but it usually is bound to the "natural" integer size of the processor in use.



回答5:

Does the size of Integer depands on Compiler or on OS or on Processor?

Yes. It can depend on any of those things.

It is actually defined by the platform ABI, which is set by the compiler and runtime libraries, but compilers use different ABIs on different OS's or architectures.



回答6:

The size of an int, and pretty much every other type, in C is implementation defined. Certain compilers may make guarantees on specific platforms but this is implementation dependent. It's nothing you should ever rely on