5

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).

Community
  • 1
  • 1
0x07FC
  • 523
  • 1
  • 6
  • 33
  • 1
    http://stackoverflow.com/questions/2331751/does-the-size-of-an-int-depend-on-the-compiler-and-or-processor/2331835#2331835 – Eugene S Aug 24 '11 at 18:12

6 Answers6

8

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.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
4

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

pmg
  • 106,608
  • 13
  • 126
  • 198
  • 1
    So you mean to say the size will be different for gcc running on 32 bit machine and 64 bit machine? – 0x07FC Aug 24 '11 at 18:27
  • It may be. Depends on the options used ( http://gcc.gnu.org/onlinedocs/gcc/i386-and-x86_002d64-Options.html ) – pmg Aug 24 '11 at 18:45
1

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.

progrmr
  • 75,956
  • 16
  • 112
  • 147
0

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.

Vinicius Kamakura
  • 7,665
  • 1
  • 29
  • 43
  • Except for `int` on 64 bit Intel systems. In the three major OSes, Windows, Linux and OS X, int is still 32 bit, while register size is 64 bit. The size of `long int` differs on these platforms, however. – Rudy Velthuis Aug 24 '11 at 18:25
0

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

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • This may be a silly semantic point, but in C the size of an `int` is *implementation-defined*, not *undefined*. Implementation-defined means that the implementation has to be consistent with the choice it makes and has to document its choice somewhere, whereas undefined means that all bets are off and the compiler can inconsistently and arbitrarily decide. – templatetypedef Aug 24 '11 at 18:15
0

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.

Stephen Canon
  • 103,815
  • 19
  • 183
  • 269