shovel_boss
shovel_boss

Reputation: 139

Uses and when to use int16_t,int32_t,int64_t and respectively short int,int,long int,long

Uses and when to use int16_t, int32_t, int64_t and respectively short, int, long.
There are too many damn types in C++. For integers when is it correct to use one over the other?

Upvotes: 12

Views: 32060

Answers (5)

AdriZ
AdriZ

Reputation: 461

In MISRA-C 2004 and MISRA-C++ 2008 guidelines, the advisory is to prefer specific-length typedefs:

typedefs that indicate size and signedness should be used in place of the basic numerical types. [...]

This rule helps to clarify the size of the storage, but does not guarantee portability because of the asymmetric behaviour of integral promotion. [...]

Exception is for char type :

The plain char type shall be used only for the storage and use of character values.

However, keep in mind that the MISRA guidelines are for critical systems.

Personally, I follow these guidelines for embedded systems, not for computer applications where I simply use an int when I want an integer, letting the compiler optimize as it wants.

Upvotes: 0

deadbird11
deadbird11

Reputation: 141

A use that I have found for them is when I am bitpacking data for, say, an image compressor. Using these types that precisely specify the number of bytes can save a lot of headache, since the C++ standard does not explicitly define the number of bytes in its types, only the MIN and MAX ranges.

Upvotes: 0

user6169399
user6169399

Reputation:

This is a good question, but hard to answer.
In one line: It depends to context:

My rule of thumb:

  • I'd prefer code performance (speed: less time, then less complexity)
  • When using existing library I'd follow the library coding style (context).
  • When coding in a team I'd follow the team coding style (context).
  • When coding new things I'd use int16_t,int32_t,int64_t,.. when ever possible.

Explanation:

using int (int is system word size) in some context give you performance, but in some other context not.

I'd use uint64_t over unsigned long long because it is concise, but when ever possible.

So It depends to context

Upvotes: 3

Keith Thompson
Keith Thompson

Reputation: 263647

Use the exact-width types when you actually need an exact width. For example, int32_t is guaranteed to be exactly 32 bits wide, with no padding bits, and with a two's-complement representation. If you need all those requirements (perhaps because they're imposed by an external data format), use int32_t. Likewise for the other [u]intN_t types.

If you merely need a signed integer type of at least 32 bits, use int_least32_t or int_fast32_t, depending on whether you want to optimize for size or speed. (They're likely to be the same type.)

Use the predefined types short, int, long, et al when they're good enough for your purposes and you don't want to use the longer names. short and int are both guaranteed to be at least 16 bits, long at least 32 bits, and long long at least 64 bits. int is normally the "natural" integer type suggested by the system's architecture; you can think of it as int_fast16_t, and long as int_fast32_t, though they're not guaranteed to be the same.

I haven't given firm criteria for using the built-in vs. the [u]int_leastN_t and [u]int_fastN_t types because, frankly, there are no such criteria. If the choice isn't imposed by the API you're using or by your organization's coding standard, it's really a matter of personal taste. Just try to be consistent.

Upvotes: 5

Blair Houghton
Blair Houghton

Reputation: 487

Use the well-defined types when the precision is important. Use the less-determinate ones when it is not. It's never wrong to use the more precise ones. It sometimes leads to bugs when you use the flexible ones.

Upvotes: 9

Related Questions