2

I have just heard that the size of an int is machine dependant. I have always thought that the size of an int is 32 bits. Can someone explain this conundrum?

Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304
  • 1
    `int` is a synonym for `System.Int32`, which is always exactly 32-bits. `long` / `System.Int64` is C#'s 64-bit integer data type. – JosephHirn Feb 15 '13 at 17:38

4 Answers4

18

In C#, the int type is always an Int32, and is always 32 bits.

Some other languages have different rules, and int can be machine dependent. In certain languages, int is defined as having a minimum size, but a machine/implementation specific size at least that large. For example, in C++, the int datatype is not necessarily 32 bits. From fundimental data types:

the general specification is that int has the natural size suggested by the system architecture (one "word") and the four integer types char, short, int and long must each one be at least as large as the one preceding it, with char being always one byte in size.

However, .NET standardized this, so the types are actually specified as Int32, Int64, etc. In C#, int is an alias for System.Int32, and will always be 32 bits. This is guaranteed by section 4.1.5 Integral Types within the C# Language Specification, which states:

The int type represents signed 32-bit integers with values between –2147483648 and 2147483647.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 1
    although if you specifically want a 64 bit integer, you can use `long` or `Int64` – DiskJunky Feb 15 '13 at 17:39
  • 1
    @DiskJunky Yes, but that's not an `int` :) – Reed Copsey Feb 15 '13 at 17:40
  • 1
    true but it was implicit in the question that there was abiguity between 32 bit and 64 bit integers, presumably on 64 bit processors :-) Although, possibly 16 bit processors too if compiling for device but we're going *way* off topic at this point. Disregard! :-) – DiskJunky Feb 15 '13 at 17:42
3

MSDN states that int is 32 bits. Was the person talking about C++ perhaps?

In C++ size of integral types in bytes is unspecified by the standard

Community
  • 1
  • 1
Karthik T
  • 31,456
  • 5
  • 68
  • 87
2

From the C# Language Specifications:

The int type represents signed 32-bit integers with values between –2147483648 and 2147483647.

System Down
  • 6,192
  • 1
  • 30
  • 34
1

int is an alias of System.Int32. You also can use System.Int16, System.Int64 and BigInteger.

Arman Hayots
  • 2,459
  • 6
  • 29
  • 53