Foredoomed
Foredoomed

Reputation: 2239

Under what circumstances can long int be used in C

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?

Upvotes: 0

Views: 4018

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 754080

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.

Upvotes: 0

autistic
autistic

Reputation: 15642

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.

Upvotes: 5

Related Questions