How do you decide which integer type to use?

Questions by lakshmi5783

Showing Answers 1 - 6 of 6 Answers

kbjarnason

  • Jul 1st, 2010
 

The first consideration is the range of values to be stored.  If they're beyond the minimum required range of, say, an int, then you can't use an int - use a long (or long long, or other int type for C99).

Part of the consideration of range is the need (or lack thereof) for negative values; if you need them use a signed type.  If you don't need them, but need a slightly greater range of positive values, use an unsigned type.

Lastly, wherever possible, use int or unsigned int (depending on range requirements) for performance reasons, or a smaller type (short, char) if the pressing issue is memory consumption.

Or, more briefly, use the type which covers the range needed, with a preference for int or unsigned int, and choose smaller types if resources are tight and they will handle the needed range.

The reason for preferring int/unsigned int over a smaller type as a general rule is that int/unsigned int are generally accepted to be the "native" size of the execution environment, and thus have the greatest expectation of being the best choice in terms of performance.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions