In C#
int type variable take 4 byte
memory and in c++
it take 2 byte
memory. Even in c++ short
and int
both take 2 byte
memory and long
takes 4 byte
. And in C# the short and int just take 4 byte. Why is this difference of memory in both languages while both follow the OOPS?

- 3,050
- 7
- 38
- 57
-
6beacuse that's how they are defined... – Mitch Wheat Aug 03 '11 at 12:29
-
1'In C# int type variable take 4 byte memory and in c++ it take 2 byte': ints in c++ are often 4 bytes... It's somewhat implementation specific. – forsvarir Aug 03 '11 at 12:31
-
[size of int, long, etc](http://stackoverflow.com/questions/589575/size-of-int-long-etc) and [What is the difference between an int and a long in C++?](http://stackoverflow.com/questions/271076/what-is-the-difference-between-an-int-and-a-long-in-c/271132#271132) – crashmstr Aug 03 '11 at 12:32
-
What compiler are you using? Typically int is 4 bytes. – Aug 03 '11 at 12:36
-
@Code Monkey: Pretty sure Turbo C++ used to have a 2 byte int... but it's been a while. – forsvarir Aug 03 '11 at 12:39
-
Sizes of integer types in C++ aren't so well defined that you seem to think. There is a minimum but no maximum. It is quite common to have int as a 32 bits type; there have been implementations where all types were 64 bits. – AProgrammer Aug 03 '11 at 12:42
-
Indeed. I think you could, theoretically, have a legal C++ implementation where char, short, int, long, long long etc were all the same size. (And that size could, theoretically, be 9 bits or 13 bits or 25 bits without breaking spec.) – LukeH Aug 03 '11 at 13:05
3 Answers
An integer that takes 2 bytes can only have 65536 different values. They are just different views on the same thing, this would mean that in the implementation of C++ that you are using:
C++ C#
1 byte byte byte
2 bytes int/short short
4 bytes long int
etc.
C++ is not more memory efficient on these numbers, the keywords just have different meaning.

- 26,772
- 8
- 53
- 86
In C#:
- int is always System.Int32
- short is always System.Int16
- long is always System.Int64
And this is because C# is compiled to CIL.
In C++ it depends on the architecture:
On 32-bit:
int and long are usually 4-bytes
short is usually 2-bytes
On 64-bit depends on the platform but I haven't ever seen 2-byte int in C++.
The main difference is compilation into native code in C++ and compilation into CIL in C#.

- 815
- 1
- 9
- 16
-
1In the olden days, there were 16 bit CPUs... and compilers to match. – forsvarir Aug 03 '11 at 12:54
On platforms where C# has a 4 byte int, C++ has that as well.
The difference is that C++ can also run on different platforms, with other sizes for the built in types.

- 90,663
- 31
- 146
- 203
-
Assuming 'platforms' refers to OS, this answer seems wrong. This http://answers.microsoft.com/en-us/windows/forum/windows_7-windows_programs/run-turbo-c-ide-in-windows-7-with-fullscreen/e68e985b-af44-4b2f-b268-78251ab2bed5 suggests you can run Turbo c++ on windows 7. This http://www.ousob.com/ng/borcpp/nga434c.php suggests that for turbo c++, the size of an int is 2 bytes. The C# equivalent for windows 7 would be 4 bytes. I think it's dangerous to suggest a correlation between the int sizes between the two languages. – forsvarir Aug 03 '11 at 12:48
-
:-) If I add a VM to Windows, I can run anything. The point was rather that C# only runs on .NET, so it only needs to have one size. – Bo Persson Aug 03 '11 at 12:54
-
'I can run anything'... Now that's a bold statement! But I take your point, .Net's your platform :) – forsvarir Aug 03 '11 at 12:59