2

Possible Duplicate:
size of int, long, etc
Is `long` guaranteed to be at least 32 bits?

I wanted to find out maximum of each data type for my computer. the code is :

int main() {
    using namespace std;
    cout << numeric_limits<int>::max() << endl;
    cout << numeric_limits<long>::max() << endl;
    cout << numeric_limits<long long>::max() << endl;

    return 0;
}

which prints:

2147483647
2147483647
9223372036854775807

question 1: why int and long are same?

question 2: above output is from VS2010 on 64bit. Is my c++ program running as 64bit?

Community
  • 1
  • 1
thkang
  • 11,215
  • 14
  • 67
  • 83
  • 8
    There aren't any guarantees on the bit width apart from, I believe, `sizeof(long) >= sizeof(int)` and `sizeof(long long) >= sizeof(long)`. – chris Dec 16 '12 at 21:40
  • Yes, your program may be running as a 64-bit executable, `int` is still 32 bit traditionally. – Dmytro Sirenko Dec 16 '12 at 21:43
  • For backwards compatibility purposes, Microsoft chose to make longs the same size as ints in 64-bit programs. We don't know if your program is 64-bit. The max values for int, long and long long will be the same for 32- and 64-bit programs compiled by VS. – Carey Gregory Dec 16 '12 at 21:43
  • @chris: There is also the guarantee that `int` represent ±32,767, `long` represent ±2,147,483,647, and `long long` represent ±9,223,372,036,854,775,807. – Dietrich Epp Dec 16 '12 at 21:49
  • @DietrichEpp, I've been trying to find that in N3485, but can't. The link in the other question was a C draft. Is it still there in C++? It's a bit confusing because it even has this somewhere else: `constexpr long long_max() { return 2147483647; } // OK` – chris Dec 16 '12 at 21:54
  • @chris: I thought the C++ spec guaranteed some certain compatibility with C and referred to the C spec. For example, `std::printf()` is not actually defined in the C++ spec. I don't know my way around the C++ spec though. – Dietrich Epp Dec 16 '12 at 22:07
  • @DietrichEpp, I think it partially does, but I'm not sure what all is included in that. – chris Dec 16 '12 at 22:08
  • 1
    @chris: In the section "numeric_limits members" it says `min()` is "Equivalent to `CHAR_MIN`, `SHRT_MIN`, `FLT_MIN`, `DBL_MIN`, etc." Those are defined by reference to the C standard. – Dietrich Epp Dec 16 '12 at 22:17
  • @DietrichEpp, Ah, a bit obfuscated imo, but thanks. – chris Dec 16 '12 at 22:20
  • Related question [here](http://stackoverflow.com/questions/10053113/is-c11s-long-long-really-at-least-64-bits). In C++11 `long long` is guaranteed to be at least 64 bits. – juanchopanza Dec 16 '12 at 22:58
  • @chris: There are *some* guarantees. See [this](http://stackoverflow.com/questions/4329777/is-long-guaranteed-to-be-at-least-32-bits) – John Dibling Dec 17 '12 at 13:38
  • @JohnDibling and juanchopanza, Thanks a lot. I never knew those questions existed. – chris Dec 17 '12 at 15:30

4 Answers4

6

question 1: why int and long are same?

For various reasons (convenience), sizes of integers supported by machine architectures tend to be powers of two. Most modern processors can natively work with 8, 16, 32, and 64-bit integers. However, there are five commonly used integer types: char, short, int, long, and long long. So two of them have to have the same size.

  • On most 16-bit platforms, int and short are both 16-bit.

  • On most 32-bit platforms, int and long are both 32-bit.

  • On most 64-bit platforms, long and long long are both 64-bit. There is one exception...

question 2: above output is from VS2010 on 64bit. Is my c++ program running as 64bit?

Impossible to tell from this data. Windows is the one platform where long and int have the same size for both 32-bit and 64-bit programs.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
2

They output the same because with Visual Studio, both int and long are signed 32-bit integers. This is true regardless of whether you're building a 64-bit or 32-bit binary (Windows 64-bit follows the LLP64 model)

Because the size of int and long doesn't vary between 32-bit and 64-bit on visual studio, there's no way to tell which you're buinding from the data you've provided

je4d
  • 7,628
  • 32
  • 46
0

The maximum value of a given type is compiler dependant. The C++ standard does not state anything about a long having to be a specific number of bits etc.

However what is does state is that:

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

If you are looking to use a specific sized integer, I would suggest including "inttypes.h" and using the int_8t, int16_t, int32_t, int64_t, etc...

DanChianucci
  • 1,175
  • 2
  • 11
  • 21
  • 2
    Actually, it guarantees that `sizeof(char) == 1`. The standard also defines "byte" as "whatever `char` is", whether or not that happens to be 8 bits. – Dietrich Epp Dec 16 '12 at 21:50
  • Neat, I did know the first part, the second part I was unaware of. I fixed my answer to reflect this. – DanChianucci Dec 16 '12 at 21:53
0

According to the C++ standard long needs to be at least as large as an int. This means that long can also be the same size as an int, which then implies that the min and max for a long and int of the same type (signed or unsigned) can be equal.

More info (not just about long and int, but other types as well) here: https://stackoverflow.com/a/271132/13760

Here is some more interesting reading: Is `long` guaranteed to be at least 32 bits?

As far as question 2 is concerned, you need to check your project settings and see if you're building a Win32 or x64 executable.

Community
  • 1
  • 1
Carl
  • 43,122
  • 10
  • 80
  • 104