Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Difference between int and long

Status
Not open for further replies.

Leijonamieli

Programmer
Nov 28, 2005
6
0
0
FI
If the size of int is 4 bytes and long is also 4 bytes, what makes long different from int? 1 byte can have 2^8 different values so 4 bytes would be 2^32, but I thought long was 2^64. Or have I missunderstood something...
 
int is machine dependent. It is basically the most machine efficient register size. For instance, on a 16 bit machine line 8086, it is 16 bits. On a 32 bit machine line a Pentium 4, it is 32 bits. On an old CDC it was 60 bits.

If you have common code that you wish to port between 16 and 32 bits, if you keep to long or short, those parts of the code will not change in size when you move between platforms. If you use int, you will probably get completely different warnings when switching between platforms.
 
Doesn't it also depend on whether your OS is 16 or 32 bits...? I always thought an int was 16 bits on DOS and 32 bits on Win95 and higher.
 
In real world programming languages standards (except Java;) no fixed short/int/long sizes specified. They used terms as long range is not less than int etc, plus max int is not less than 32767.

Moreover, the only standard sizeof value specification in C/C++ is sizeof(char) == 1. For example, you may have sizeof(long) == 1 in some standard implementation.
 
Thank you for explaining this. I have previously programmed Java so I made a wrong assumption.
 
ArkM: I'm not sure if that's right? Here's what I found on the MSDN site:
The int and unsigned int types have the size of the system word. This is two bytes (the same as short and unsigned short) in MS-DOS and 16-bit versions of Windows, and 4 bytes in 32-bit operating systems. However, portable code should not depend on the size of int.
I'm pretty sure I saw other sources talking about int being the size of a system word also, but I can't remember where...
 
Humm, maybe I don't remember this OK, but I have the idea in my mind that is compiler dependant.

In fact, my very first Pentium had w98 and Linux and I remember the int size was different. Int was a short for Borland DOS compiler and int was a long for GNU gcc.

Cheers,
Dian
 
cpjust:
That's the sizes MS have defined for their compilers running under those OSes on x86 machines. Other environments may be different.
 
of course, the solution to all of these problems is to create a typedefs header, that way when standard technology moves from 32bit to 64bit, you wont have to change old code. its something i like to use, and its something you wont have to worry about in the future, also it helps when youre reading your code to have the consistency from project to project.
 
cpjust:
See KempCGDR's post. See also:
C++ Std said:
Certain aspects and operations of the abstract machine are described in this International Standard as implementation-defined (for example, sizeof(int)). These constitute the parameters of the abstract machine. Each implementation shall include documentation describing its characteristics and behavior in these respects.
The 2nd quote:
C++ Std said:
Plain ints have the natural size suggested by the architecture of the execution environment;...
The natural size, that's all.
C++ Std-conformed preprocessor must recognize integers in range [-32767..32767].
 
I know about int's changing their sizes on different platforms... but everything I've ever heard says that a short is always 16-bits and a long is always 32-bits in C/C++.
 
precision(short) <= precision(int) <= precision(long).
precision(int) >= 16 bit.
That's all rules...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top