Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when would you use uint_least16_t

Tags:

c

I'm looking at stdint.h and given that it has uint16_t and uint_fast16_t, what is the use for uint_least16_t what might you want that couldn't be done equally well with one of the other two?

like image 641
Gordon Wrigley Avatar asked Oct 30 '08 07:10

Gordon Wrigley


People also ask

What is uint_least16_t?

uint_least16_t the smallest thing that is capable of holding a uint16. uint_fast16_t the fastest thing that is capable of holding a uint16. uint16_t exactly a uint16, unfortunately may not be available on all platforms, on any platform where is is available uint_least16_t will refer to it.

What is the differentiating aspect between the two numeric types short and uint16_t?

However, uint16_t says that you must be given an integer that is unsigned and exactly 16 bits. Unsigned short says that you will be given an unsigned value that is at least 16 bits, but could be more than 16 bits. This is more useful for int32_t, uint32_t, int64_t, and uint64_t since those vary more by the system.

What does uint16_ t mean in C?

uint16_t is unsigned 16-bit integer. unsigned short int is unsigned short integer, but the size is implementation dependent. The standard only says it's at least 16-bit (i.e, minimum value of UINT_MAX is 65535 ).

What is uint_ fast16_ t?

uint_fast16_t should be used when you're writing code for C99 (or later) that needs an unsigned integer type that is at least 16 bits wide, chosen to make operations as fast as possible.


2 Answers

Say you're working on a compiler with:

  • unsigned char is 8 bits
  • unsigned short is 32 bits
  • unsigned int is 64 bits

And unsigned int is the 'fastest'. On that platform:

  • uint16_t would not be available
  • uint_least16_t would be a 32 bit value
  • uint_fast16_t would be a 64 bit value

A bit arcane, but that's what it's for.

How useful they are is another story - I see the exact size variants all the time. That's what people want. The 'least' and 'fast' versions I've seen used pretty close to never (it's possible that it was only in example code - I'm really not sure).

like image 50
Michael Burr Avatar answered Oct 18 '22 07:10

Michael Burr


Ah, the link Patrick posted includes this "The typedef name uint_leastN_t designates an unsigned integer type with a width of at least N, such that no unsigned integer type with lesser size has at least the specified width."

So my current understanding is:

uint_least16_t the smallest thing that is capable of holding a uint16

uint_fast16_t the fastest thing that is capable of holding a uint16

uint16_t exactly a uint16, unfortunately may not be available on all platforms, on any platform where is is available uint_least16_t will refer to it. So if it were guaranteed to exist on all platforms we wouldn't need uint_least16_t at all.

like image 23
Gordon Wrigley Avatar answered Oct 18 '22 06:10

Gordon Wrigley