Why in Turbo C compiler sizeof(int) is 2 bytes and in gcc Linux compiler is of 4 bytes?
sizeof(int) is not a constant across all platforms.
It varies from system to system.
PS: Only sizeof object which is constant across all platforms is sizeof(char)
sizeof(int) varies from machine to machine (and sometimes from compiler to compiler).
Usually sizeof(int) represents the "natural" word width of the CPU. But If your compiler runs as x86 program on a x64 machine even this assumption breaks.
In MSDOS instructions codes are 16 bit | 2 Bytes.
So, the maximum integer values would be 16bit integers.
The keyword
intdiffers from compiler to compiler. Turbo C is a16 bitcompiler so it compiles the code into a16 bitmachine code for the processor!As we all know, a compiler will convert the code to a machine code in order to work.
Same applies for GCC.
The computers we use today is either 32/64 bit.
A compiler should support the architecture to make any applications work.
32/64 bit compiler. So, sizeof(int) is 4 Bytes.16bit compiler. So, sizeof(int) is 2 Bytes.