tags:

views:

308

answers:

9

Possible Duplicate:
What is the difference between an int and a long in C++?

in C++, why is there the need of datatypes int and long int. Both occupy same memory (4 bytes) in 32 bit system

i hav simliar doubt , y is there char and wchar_t....y cant we use one datatype both intake similar values

+20  A: 

They are not always the same size! In fact, the C++ standard does not explicitly specify exact sizes for any integral datatype. int is meant to be the "natural" size for the processor, while long int is a datatype that just happens to be 4 bytes on the compiler you're using. The standard only guarantees that a long int has at least as much bits as an int.

C++ standard 3.9.1-2:

There are four signed integer types: "signed char", "short int", "int", and "long int." In this list, each type provides at least as much storage as those preceding it in the list. Plain ints have the natural size suggested by the architecture of the execution environment; the other signed integer types are provided to meet special needs.

Addendum: To address your comment about char and wchar_t:

C++ standard 3.9.1-1:

Objects declared as characters (char) shall be large enough to store any member of the implementation's basic character set...

C++ standard 3.9.1-5:

Type wchar_t is a distinct type whose values can represent distinct codes for all members of the largest extended character set specified among the supported locales...

So they are considered different types by the C++ standard, with different purposes.

In silico
+1: Note that "natural size" does not mean "the fastest to work with". That's why `fastint_t` was added in C99;
Billy ONeal
A: 

int occupies 4 bytes in 32 bit machines. But in old 16 bit machine, it is 2 bytes. The rule is short is always 16 bit, long is always 32 bit and int is 16 bit or 32 bit depending on the system.

-- edit
As specified in the comment, this is not the case for 64 bit system. Thanks for pointing me my mistake.

taskinoor
-1 not true. On a 64-bit machine, long can be 64-bits. The only standard rule is that short is *at least* 16 bits, int is *at least* 16 bits, and long is *at least* 32 bits.
Nick Meyer
If you replace "rule" by "Windows convention", then it becomes correct.
Philipp
@Nick: Your rule allows 32-bit short int and 16-bit int, the standard does not.
Ben Voigt
@Ben thanks for pointing that out. I guess I left out that sizeof(int) >= sizeof(short) and sizeof(long) >= sizeof(int).
Nick Meyer
+1  A: 

In java, int is 32 bit, long is 64 bit. They are not the same.

BobTurbo
Question is about C++
Mark
The question was edited. It first also included java. Please see the edits.
BobTurbo
@Mark Actually, originally when BobTurbo wrote his answer it was tagged both C++ and Java.
janneb
+1  A: 

On some platforms, the premise is incorrect. E.g. Linux and other Unix machines in 64-bit mode typically have int as a 32-bit integer while long is a 64-bit integer.

janneb
+1  A: 

int was 16 bits wide on 16-bit Windows systems, and long is 64 bits wide on Unix-like 64-bit systems. So they are not the same.

Philipp
+4  A: 

Both have occupy same memory

That's a wrong statement. Maybe it does on your system, but the C standard does not specify the length of the short int, int and long int data types.
At least two of them need to be different, but it's not specified which one. They may also have different length.

So it's really implementation defined.

EDIT

Here's what the C89 specification says:

'int' and 'short' must be at least 16 bits and 'long' be at least as long as 'int' and not smaller than 32 bits.

For instance, on Mac OS X 10.5, short is 16 bits, int 32 bits, and long 32 bits.
Since Mac OS X 10.6, short is 16 bits, int 32 bits, and long 64 bits.

Macmade
See the edit. : )
Macmade
Thanks. All 3 could still be 32 bits though, according to the spec.
Justin Ardini
A: 

Found this on http://www.cplusplus.com/doc/tutorial/variables/

The values Size and Range depend on the system the program is compiled for. 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. The same applies to the floating point types float, double and long double, where each one must provide at least as much precision as the preceding one.

subanki
A: 

C++ standard do not define exact size of the int and long. It is up to the hardware and operating system.

On some 64 bits (e.g. Linux 64-bits) system the long take 8 bytes (64-bits).

Simply saying, the int or long will change depend on where the target platform (hardware + os) it is compiled for.

For example, if you use long, when your code run on a system with larger long, you can take advantage of it.

Caution: If you are writing code that manipulate on bit and byte level remember to do sizeof() checking in your code.

ttchong