49

Is it the same? If no: what is the difference? If yes then why do you need this type?

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
user1511417
  • 1,880
  • 3
  • 20
  • 41

1 Answers1

65

uint32_t (or however pre-C++11 compilers call it) is guaranteed to be a 32-bit unsigned integer; unsigned int is whatever unsigned integer the compiler likes best to call unsigned int, as far as it meets the requirements of the standard (which demands for it a 0-65535 minimum range).

Like int, unsigned int typically is an integer that is fast to manipulate for the current architecture (normally it fits into a register), so it's to be used when a "normal", fast integer is required.

uint32_t, instead, is used when you need an exact-width integer, e.g. to serialize to file, or when you require that exact range or you rely on unsigned overflow to happen exactly at 2^32-1.

For example, on a 16 bit-processor unsigned int will typically be 16 bits wide, while uint32_t will have to be 32 bits wide.


Incidentally, as pointed out by @Marc Glisse, while unsigned int is always present, uint32_t is not mandatory - a particular compiler implementation may not provide it. This is mostly because not all platforms can easily provide such a type (typically DSPs with weird word sizes).

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • 5
    Also, one is required to exist and not the other. – Marc Glisse Feb 16 '13 at 15:33
  • @user1511417: it makes no sense to ask which is better "a priori" - they both have their use, as outlined in the answer. – Matteo Italia Feb 16 '13 at 15:35
  • The `uint32_t` is also used when exact bit width access is needed, such as writing to hardware registers. – Thomas Matthews Feb 16 '13 at 17:01
  • `uint_least32_t`, however, *is* required to exist. Doesn't help with direct hardware access, but does help with minimum width requirements. – DevSolar Oct 28 '15 at 11:59
  • 1
    "0-65535 minimum range" is a slight understatement. It must have a `[0, 2^N-1]` range with N >=16, in order to support modulo 2^N arithmetic. – MSalters Oct 28 '15 at 13:04
  • @DevSolar Although `unsigned long int` is also guaranteed to be at least 32 bits wide and has been around forever. I guess the least type would be useful on an implementation with something like 36-bit ints and `long` that’s wider. You might want to use `least` for storage and `fast` for calculations there. – Davislor Oct 28 '15 at 13:13
  • @Lorehead: Well, `unsigned long int` could be 64 bit as well, while `uint_least32_t` is also guaranteed that "no unsigned integer type with lesser size has at least the specified width". Plus, the `_t` type is explicit about its requirements, while `unsigned long` isn't. And I'd call 16 years of existence in the (C99) standard pretty much "forever" as well, even though I wrote C before that "forever". – DevSolar Oct 28 '15 at 13:19
  • @DevSolar Useful in comparison to `uint32_t`, which would exist basically everywhere except some really old mainframes that no one has ever written a C99 compiler for. (Just like how `long` was 32 bits wide everywhere and also wide enough to hold a pointer or file offset, and neither of these assumptions would ever cause problems in the future.) But, you’re right, `uint_least32_t` and `uint_fast32_t` are the most future-proof types to use. – Davislor Oct 28 '15 at 13:32