3

The size of int is 2 byte in c and 4 in c#.why is that ?. Why not 2 byte like c. i searched on the web also but i've not found anything. I am searching for the real technical reason of this.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331

2 Answers2

5

In C#, int means Int32 i.e., int is an alias for Int32. Hence int is always 32 bit which is equivalent to 4 bytes. And it was made so as to make it compatible with 32 bit and 64 bit platform. If you write Int64 then it is 8 bytes.

You can refer this MSDN: What is the size of int in c#

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
4

That’s incorrect. The size of int in C can be two, four or eight bytes depending on whether you compile the program as 16-bit, 32-bit, or ILP64, but today it’s almost always 32 bits.

The 32-bit integer type usually has the shortest and simplest name because most computers today run most efficiently when they’re working with 32 bits of data. 64-bit machines can run 32-bit code much faster than 32-bit machines can work with 64-bit numbers. While int could be, and sometimes is, defined as 64 bits on a 64-bit machine, this can introduce a lot of subtle bugs when compiling code that was written and tested on a 32-bit machine.

If you want to work with an exact number of bits, there are types in both languages that let you specify an exact width.

Davislor
  • 14,674
  • 2
  • 34
  • 49