uint32_t vs int as a convention for everyday programming

Guy picture Guy · Nov 19, 2013 · Viewed 25.7k times · Source

When should one use the datatypes from stdint.h? Is it right to always use as a convention them? What was the purpose of the design of nonspecific size types like int and short?

Answer

chux - Reinstate Monica picture chux - Reinstate Monica · Nov 19, 2013

When should one use the datatypes from stdint.h?

  1. When the programming tasks specify the integer width especially to accommodate some file or communication protocol format.
  2. When high degree of portability between platforms is required over performance.

Is it right to always use as a convention them (then)?

Things are leaning that way. The fixed width types are a more recent addition to C. Original C had char, short, int, long and that was progressive as it tried, without being too specific, to accommodate the various integer sizes available across a wide variety of processors and environments. As C is 40ish years old, it speaks to the success of that strategy. Much C code has been written and successfully copes with the soft integer specification size. With increasing needs for consistency, char, short, int, long and long long, are not enough (or at least not so easy) and so int8_t, int16_t, int32_t, int64_t are born. New languages tend to require very specific fixed integer size types and 2's complement. As they are successfully, that Darwinian pressure will push on C. My crystal ball says we will see a slow migration to increasing uses of fixed width types in C.

What was the purpose of the design of nonspecific size types like int and short?

It was a good first step to accommodate the wide variety of various integer widths (8,9,12,18,36, etc.) and encodings (2's, 1's, sign/mag). So much coding today uses power-of-2 size integers with 2's complement, that one may not realize that many other arrangements existed beforehand. See this answer also.