Difference between uint8_t, uint_fast8_t and uint_least8_t

mic picture mic · Jan 28, 2016 · Viewed 23.1k times · Source

The C99 standard introduces the following datatypes. The documentation can be found here for the AVR stdint library.

  • uint8_t means it's an 8-bit unsigned type.
  • uint_fast8_t means it's the fastest unsigned int with at least 8 bits.
  • uint_least8_t means it's an unsigned int with at least 8 bits.

I understand uint8_t and what is uint_fast8_t( I don't know how it's implemented in register level).

1.Can you explain what is the meaning of "it's an unsigned int with at least 8 bits"?

2.How uint_fast8_t and uint_least8_t help increase efficiency/code space compared to the uint8_t?

Answer

rodrigo picture rodrigo · Jan 28, 2016

uint_least8_t is the smallest type that has at least 8 bits. uint_fast8_t is the fastest type that has at least 8 bits.

You can see the differences by imagining exotic architectures. Imagine a 20-bit architecture. Its unsigned int has 20 bits (one register), and its unsigned char has 10 bits. So sizeof(int) == 2, but using char types requires extra instructions to cut the registers in half. Then:

  • uint8_t: is undefined (no 8 bit type).
  • uint_least8_t: is unsigned char, the smallest type that is at least 8 bits.
  • uint_fast8_t: is unsigned int, because in my imaginary architecture, a half-register variable is slower than a full-register one.