int vs. long

Started by
28 comments, last by Densun 22 years, 1 month ago
Why use int (the 32-bit version) when you can use long?
Advertisement
''int'' is a mere 3 letters long, whereas ''long'' is an unbearable 4 letters. Using int can save you lots of typing time in the long run.

quote:Original post by krez
just in case: you can fit twice as many 32-bit integers as 64-bit ones into the same memory...

Are you saying that a long is 64-bits? I always thought it was 32.
a long takes up 8 bytes of memory as aposed to an integer which takes 4 bytes.
int = 32 bits
long = register size, 32 bits on intel, 64 on aplha...
Um... actually.. in most c++ compilers an INT is 32-bits, and a LONG is ALWAYS 32-bits... an int can be of different sizes, so for cross compiler compatibility, I always use short for 16-bit numbers, and long for 32-bit. I try to avoid int''s whenever possible. ANSI C/C++ standard states that a short shall be 16-bit data type, and a long shall be a 32-bit data type, an int is dependent on the target. 16-bit compilers (Borlan Turbo C/C++ series for Dos for example) defined int''s as 16-bits, while MSVC (32-bit compiler) defines int''s as 32-bits... once 64-bit compilers start coming out, int''s will be defined as 64-bit, etc, etc. It''s safer to stick with short/long''s that way you know exactly what you''re getting/using. Even though it''s an extra letter for every time you type long instead of int .

Billy - BillyB@mrsnj.com
Oh, and by the way... long takes up 4 bytes, aka 32-bits.

Billy - BillyB@mrsnj.com
Opps, reminds me not to answer questions after 2hrs sleep . Had it flipped.

Int = Varies by platform
Long = 32Bits
I guess we''ll forgive you .

Billy
This comes up over & over & over & over...

The only thing the standard says is that:

sizeof(char) == 1
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)

There are no guarantees that an int is going to be 32 bits (or whatever). The only guarantee you have is that an int is at least as big as a short and no bigger than a long.

You''re not even guaranteed that sizeof(char)==1 (8-bit) *byte*. You''re only guaranteed that it is 1 *something*.

Practically speaking of course if sizeof(char) was anything other than one 8-bit byte it would break virtually every non-trivial C/C++ app written in the last 15 years or so. The same *cannot* be said of short, int, and long. The sizes of these can, and have, changed between compilers, OS''s, etc.

In MSVC on Win32 short=16 bits, int=long=32 bits, but that is only true for that specific platform.

-Mike
-Mike

This topic is closed to new replies.

Advertisement