3

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

In c++ which is the diference between the int and long data type. Wrote the following code:

int _tmain(int argc, _TCHAR* argv[])
{
    cout<<"Int_Max = "<<INT_MAX<<endl;
    cout<<"Int_Min = "<<INT_MIN<<endl;
    cout<<"Long_Max = "<<LONG_MAX<<endl;
    cout<<"Long_Min = "<<LONG_MIN<<endl;
}

And this were the results...

Int_Max = 2147483647
Int_Min = -2147483648
Long_Max = 2147483647
Long_Min = -2147483648

I'm confused.

Community
  • 1
  • 1
Yoss
  • 428
  • 6
  • 16

4 Answers4

5

According to the standard, int is guaranteed to be at least 16 bits and long is at least 32 bits. On most 32-bit compilers they are just the same, both 32 bits. But you shouldn't count on this as there are 64-, 16- and even 8-bit compilers too.

cyco130
  • 4,654
  • 25
  • 34
  • The standard also guarantees the size of a char <= short <= int <= long. Interestingly, the standard does not mandate an 8-bit char, just that a char is at least 8-bits. It's possible for a char to be 9-bits in size, which would mean that you have a 9-bit byte. – David Stone Sep 12 '11 at 20:03
3

On 32-bit systems, there's usually no difference between int and long. If you want a 64-bit datatype on a 32-bit system, try long long instead.

Michael
  • 701
  • 4
  • 15
1

If you want to guarantee the size of your data types, you should use the types in cstdint (requires C++0x support in g++, which is turned on by compiling with -std=c++0x). For instance, rather than int or long, you would use int32_t and int64_t. If you want an unsigned data type, simply add a 'u' to the beginning. uint16_t is probably equal to unsigned short on your platform. It's a 16-bit unsigned integer.

However, you should only use these where the exact space is important. Otherwise, "int" is generally preferable.

For a more complete reference, see https://en.wikipedia.org/wiki/Stdint.h which includes other options such as int_least16_t for a data type that's at least 16 bits wide, among other things.

Krenair
  • 570
  • 5
  • 21
David Stone
  • 26,872
  • 14
  • 68
  • 84
0

char : at least 8 bits : equal to one unit

short : at least 16 bits : at least as wide as char

int : at least 16 bits : at least as wide as short

long : at least 32 bits : at least as wide as int

Eric R.
  • 1,105
  • 3
  • 21
  • 48