Question

In C++ (which i am learning and am still very very new to), I have noticed that almost everybody uses the int data type. But why? I know that short, long, and long long have definite sizes pretty much, but int seems like it might be short or long depending on the system. So, why don't people be more specific about the types? If they put a number into an int that is too big for a short, then on certain systems it will be really bad. If the number you're putting into an int is small enough to fit into a short, then on systems where it defaults to long memory space is wasted. So why does everybody use int?

Was it helpful?

Solution

According to the standard (C++11, §3.9.1/2),

Plain ints have the natural size suggested by the architecture of the execution environment; the other signed integer types are provided to meet special needs.

So int is the type you should use unless you have a good reason to use any other type, because int is supposed to map to the type that the architecture is optimized to use most of the time.

OTHER TIPS

You're correct in that int has a variable size. But short, long, and long long also have variable sizes. So they aren't a better option.

I'm not going to speculate on why people use int because that would just be my opinion.

If you need an actually true sized integer though, you should use int32_t or uint32_t or int64_t. These types have required sizes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top