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

17,348

Solution 1

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.

Solution 2

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.

Share:
17,348

Related videos on Youtube

shovel_boss
Author by

shovel_boss

Am i the boss of the shovel or is the shovel a boss of me Maybe the shovel is a reference? No i don't play shovel knight I did watch shovel dog GIF once and got inspired When people dig I sell them the shovels

Updated on September 26, 2022

Comments

  • shovel_boss
    shovel_boss about 1 year

    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?

    • Scott Hunter
      Scott Hunter about 7 years
      Stick with the undamned ones.
    • Captain Obvlious
      Captain Obvlious about 7 years
      ...and the undamned zeros.
  • Pete Becker
    Pete Becker about 7 years
    It's wrong to use the "more precise" ones when they don't exist.
  • Admin
    Admin about 7 years
    caveat emptor: platform specificity matters. Make sure you use types supported across all your targets, which depending on your implementation can mean using a generic one that behaves well across the deployment matrix, or micro managing specific ones and providing consistency yourself
  • DarthRubik
    DarthRubik about 7 years
    @PeteBecker As a last ditch effort to use the "more precise" ones, you could define them for the platform you are working, when porting from a platform that did have them to one that does not.