0

While the size of int depends on the CPU, long seems to be 32 bit (?). But it seems so intuitive to use int for numbers where size doesn't really matter, like in a for loop.

It's also confusing that C++ has both long and __int32. What is the second for then?

Question: What number types should I use in what situations?

bytecode77
  • 14,163
  • 30
  • 110
  • 141
  • 10
    No, long is not always 32 bits... – Mat Apr 19 '15 at 12:11
  • long is not smaller than int. At least 32 bits. – Alper Apr 19 '15 at 12:12
  • I really like C# for the data-types. They are always the same, everywhere. In C++, I'm not even sure anymore if I should use `int` at all. – bytecode77 Apr 19 '15 at 12:13
  • 3
    There are [fixed-size integers](http://en.cppreference.com/w/cpp/types/integer) in the standard library. – Some programmer dude Apr 19 '15 at 12:15
  • if C++ standard mandates the fixed size for types then creating a compiler 12, 18, 24, 36, etc-bit systems would be a pain. `__int32` and `__int64` is not a standard type – phuclv Apr 19 '15 at 12:21
  • `int` should always the default type because all narrower types would be promoted to it or `unsigned int`, unless you need at least 64 bits http://stackoverflow.com/questions/6825023/should-i-use-long-instead-of-int-on-64-bits-in-langs-with-fixed-type-size-l On most systems operations on int will be at least equal or sometimes better than long. If long is 64 bits then on x86 they'll also need longer instructions – phuclv Apr 19 '15 at 12:31

3 Answers3

5

Both int and long don´t have fixed sizes (or any fixed representation at all), as long they can hold specific value ranges (including that long can´t be smaller than int).

For specific sizes, there are some types like int32_t etc. (which may be the same).

And __int32 isn´t standard C++, but a compiler-specific thing (eg. MSVC)

deviantfan
  • 11,268
  • 3
  • 32
  • 49
  • Why did Microsoft add `__int32`, if it's the same as the standard `int32_t`? – bytecode77 Apr 19 '15 at 12:14
  • I think that int32_t implemented ad typedef of __int32 – gomons Apr 19 '15 at 12:15
  • What should I use if I want exactly 32 bit then? – bytecode77 Apr 19 '15 at 12:16
  • I mean that __int32 is native type for msvc compiler, and standard types implemented through native types – gomons Apr 19 '15 at 12:16
  • 1
    @bytecode77 `Why did Microsoft add __int32` Who knows...but MSVC has so much non-standard things (while "forgetting" to implement some standard stuff for about 16 years...) – deviantfan Apr 19 '15 at 12:16
  • `What should I use if I want exactly 32 bit then?` `int32_t` (which may not exist on some strange hardware platforms) – deviantfan Apr 19 '15 at 12:17
  • you can find type int32_t in every compiler that implements c++ standard, so you can compile code using both msvc, gcc (mingw), it is cross-platform aproach – gomons Apr 19 '15 at 12:17
  • I belive `_int32` was added before `int32_t` was introduced. – Mr M. Apr 19 '15 at 12:18
  • When sticking to MSVC and all of its non-standard stuff, is `__int32` a "prettier" option? I assume so because it's highlighed as a keyword rather than a class. – bytecode77 Apr 19 '15 at 12:19
  • @bytecode77 `int32_t` isn´t a class, it´s a "normal" int type too. And no. While MSVC has problems with fulfilling the requirements, it knows `int32_t`, and using compiler-specific things if you have independent alternatives will make your code harder to use on other systems, nothing more. – deviantfan Apr 19 '15 at 12:22
  • I believe `__int32` is used when exact width is needed, because this predates the fixed-width types in C99 and C++11 and in DOS and 16-bit windows, int has only 16 bits – phuclv Apr 19 '15 at 12:23
  • @LưuVĩnhPhúc So you´re saying that we should use `__int32` if using such old MSVC versions that it doesn´t know anything of C99 yet. Well, yes, but I´m assuming new tools here. – deviantfan Apr 19 '15 at 12:26
  • yes, but MSVC compiler has been there a long time. Even before long long exists, so they created `__int64` – phuclv Apr 19 '15 at 12:28
  • @LưuVĩnhPhúc Well, yes...? I still don´t see a reason to use this types when writing new code in new VS versions. – deviantfan Apr 19 '15 at 12:29
  • did I say you must use it in new code? They're created for MS (historical) purposes – phuclv Apr 19 '15 at 12:34
2

Standard specifies, that long is not shorter than int - Specified in C++ standard §3.9.1

C++11 introduced integers with fixed number of bytes, like int32_t.

Mr M.
  • 715
  • 1
  • 8
  • 24
-2

Note that int is 32 bits even on many 64-bit architecture/compiler combinations (the 64-bit versions of gcc and MSVC both use 32 bits, as far as I know). On the other hand, long will typically be 64 bits on a 64-bit compiler (not on Windows, though).

These are only guidelines though, you always have to look into your compiler's manual to find out how these datatypes are defined.

cfh
  • 4,576
  • 1
  • 24
  • 34