0

In my machine, i get the following results:

sizeof(long) = 8

sizeof(long int) = 8

Where to use long int and why not just use int?

Mohit Kanwar
  • 2,962
  • 7
  • 39
  • 59
Foredoomed
  • 2,219
  • 2
  • 21
  • 39
  • When you need something that's at least 32 bits? – chris Apr 08 '13 at 04:41
  • 6
    Maybe you meant *use long int and why not just use **long***. – cnicutar Apr 08 '13 at 04:42
  • 3
    `long` is just shorthand for `long int`, they're exactly the same. Further example/trivia: in the same manner, you can have a declaration `unsigned a;`, being shorthand for `unsigned int a;` – Mac Apr 08 '13 at 04:42
  • Assuming the intended question is "why use `long int` instead of just `long`", the answer is clarity. There are many programmers who would be confused if they see a variable declared as just `long` instead of `long int`. Many people would argue that it is perfectly acceptable to just use `long`, and many people prefer that style. It is purely a stylistic choice. – William Pursell Apr 08 '13 at 04:49
  • @cnicutar: that's exactly what I assumed (hence my earlier comment), to the point that it took me two or three times reading your comment before I realised the OP hadn't written what I thought (s)he had... :) – Mac Apr 08 '13 at 04:53
  • "There are many programmers who would be confused if they see a variable declared as just long instead of long int. " -- Then there must be many confused programmers, since `long` is by far the more common usage. – Jim Balter Apr 08 '13 at 08:22

2 Answers2

5

As indicated in your comments, the example you provided isn't relevant to the question you asked. If your question was meant to be:

So my question is where to use long int and why not just use long?

... then the answer is short: Use whichever you choose, as they're equivalent.

Here's the question you asked, followed by it's answer:

So my question is where to use long int and why not just use int?

int is guaranteed to be able to store, at the very least, values that lie within the range of -32767 and 32767. Implementations might make decisions that allow it to store values outside that range, but aren't required to.

long int or long is guaranteed to be able to store, at the very least, values that lie within the range of -2147483647 and 2147483647. Again, implementations might make decisions that allow it to store values outside that range, but aren't required to.

Hence, if you're interested in developing portable software, it would make sense that you use int for objects that aren't expected to store values that fall outside of that minimum range (-32767 .. 32767) for int. Ditto for long.

If you're not interested in developing portable software, and you only care about your own implementation, use whichever type fulfills your requirements best. You can obtain your implementations range of int by reviewing INT_MIN and INT_MAX from <limits.h>, and long by reviewing LONG_MIN and LONG_MAX.

Make note that the sizeof an integer type isn't necessarily an accurate reflection of its range, due to the potential for padding bits and negative zeros to exist.

autistic
  • 1
  • 3
  • 35
  • 80
  • To add for this, note the difference between the results in the question and those of codepad's C libraries: http://codepad.org/hW92RusA. While "sizeof" both int and long int now show "4" in codepad, they are still able to handle the range of numbers required by the C standard, as noted by modifiable lvalue in the above post. – Marshall Conover Apr 08 '13 at 05:41
  • @MarshallConover `sizeof (short)` could be 1, and that alone would tell you nothing about the range of values a `short` can represent. – autistic Apr 08 '13 at 05:54
  • How could you represent 32767, which I understand is within the C standard requirement for something being a "short," with only 8 bits? Or am I misunderstanding what "sizeof" returns? – Marshall Conover Apr 08 '13 at 15:24
  • 1
    @MarshallConover `sizeof` doesn't return anything. `sizeof` is an operator, like the `&address-of` operator. Operators and operands form expressions, which evaluate. Expressions where the operator is `sizeof` evaluate to the number of `char`s in a type or the type of an expression. The number of bits in a `char` (`CHAR_BIT` from ``) is required to be at least 8, but might be more. For example, see [this question](http://stackoverflow.com/questions/2098149/what-platforms-have-something-other-than-8-bit-char). Thus, `sizeof (short)` may very well evaluate to 1 when `CHAR_BIT >= 16`. – autistic Apr 08 '13 at 15:44
  • Oh wow, thanks for explaining that, and for the link - I didn't know sizeof uses CHAR as its unit, and that CHAR_BIT can change. Thanks! – Marshall Conover Apr 08 '13 at 16:41
0

Note that even before you permute the words, all of the following denote the same type:

  • long
  • long int
  • signed long int
  • signed long

With permutations, you could have:

long            l0;
long int        l1;
int long        l2;

signed long int l3;
signed int long l4;
long signed int l5;
long int signed l6;
int signed long l7;
int long signed l8;

signed long     l9;
long signed     lA;

Most sane people, most of the time, simply write long to denote this type.

Your actual question is about when to use long int and when to use int. Technically, the range of values guaranteed by int is ±32,767 (or ±(215-1)). If you need values bigger than that, you should use long. However, you often find that int has a range of ±(231-1) (more or less). Sometimes, the range of long is the same as int (32-bit systems; 64-bit Windows); then the main reason for using long or int is to match the interface to a particular API (because the types are still distinct, even if they support the same range). Sometimes, the range of long is ±(263-1) (64-bit systems apart from 64-bit Windows); then you'd use long if you needed a range bigger than int. However, you might also consider using the 'portable' types from <inttypes.h> or <stdint.h> such as int32_t and int64_t instead. However, if the API uses a different type, you should probably use the API's type.

If you intended to ask about when to use long and long int, then it is a matter of taste. I'd use plain long without any qualms. Unless there was a compelling reason for symmetry with some other declaration, I'd not use anything else.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278