4

Why in Turbo C compiler sizeof(int) is 2 bytes and in gcc Linux compiler is of 4 bytes?

alk
  • 69,737
  • 10
  • 105
  • 255
Mukul
  • 114
  • 1
  • 11
  • 3
    Don't use TurboC. It is obsolete, non-standard conforming, gives bad diagnostics and produces poor code. Use some good free software compilers like [GCC](http://gcc.gnu.org/) or [Clang/LLVM](http://clang.llvm.org/). Consider installing Linux on your PC if you don't have it yet. – Basile Starynkevitch Jul 13 '15 at 06:49
  • Why all these downvotes? It's legitimate question, isn't it? – alk Jul 13 '15 at 08:31
  • Related: http://stackoverflow.com/q/14159971/694576 – alk Jul 13 '15 at 08:37
  • @alk I don't get the down-votes for valid questions – Gopi Jul 13 '15 at 08:44
  • I would answer if allowed. An `int` was originally supposed to be the native word size of the machine, and Turbo C produced code for 8086 and 80286 machines with no 32-bit instructions. There are, in fact, also GCC targets for Linux on 16-bit machines. – Davislor Oct 05 '18 at 06:04
  • GCC keeps 32-bit `int` even on `linux-x86_64` for three major reasons: that’s a little faster (mostly due to more `int` variables fitting in the cache), it’s backward-compatible with 32-bit Linux, and `int` is not allowed to be longer than `long int` (any more). There’s a *lot* of legacy code out there that assumes `long int` is exactly 32 bits wide, improperly. With a few defunct exceptions in the last century, compiler writers for 64-bit UNIX-like OSes just gave in and kept `int` and `long int` 32 bits wide. (POSIX requires that there be a switch to make `long` as wide as a pointer.) – Davislor Oct 05 '18 at 06:11

3 Answers3

6

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)

Gopi
  • 19,784
  • 4
  • 24
  • 36
0

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.

DrKoch
  • 9,556
  • 2
  • 34
  • 43
  • "*... `sizeof(int)` represents ...*" 1st this is not true as you add yourself, 2nd this does not answer the question. – alk Jul 13 '15 at 08:35
  • 1
    @alk Please see the word "Usually". Do you know many counter-examples? – DrKoch Jul 13 '15 at 08:59
0

In MSDOS instructions codes are 16 bit | 2 Bytes.
So, the maximum integer values would be 16bit integers.

What I've analysed so far:

The keyword int differs from compiler to compiler. Turbo C is a 16 bit compiler so it compiles the code into a 16 bit machine 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.

  • GCC is 32/64 bit compiler. So, sizeof(int) is 4 Bytes.
  • Turbo C is 16bit compiler. So, sizeof(int) is 2 Bytes.
Community
  • 1
  • 1