6

From ISO/IEC 9899:

7.18.1.2 Minimum-width integer types

1 The typedef name int_leastN_t designates a signed integer type with a width of at least N, such that no signed integer type with lesser size has at least the specified width. Thus, int_least32_t denotes a signed integer type with a width of at least 32 bits.

Why I should ever use this types?

When I'm deciding what type I should take for a variable I need, then I ask my self: "What will be the biggest value it could ever be carrying?"

So I'm going to find an answer, check what's the lowest 2n which is bigger than that, and take the matching exact integer type.

So in this case I could use also a minimum-width integer type. But why? As I already know: it will never be a greater value. So why take something that could sometimes cover even more as I need?

All other cases I can imagin of where even invalid as i.e.:

"I have a type that will be at least size of..." - The implmentation can't know what will be the largest (for example) user input I will ever get, so adjusting the type at compile time won't help.

"I have a variable where I can't determine what size of values it will be holding on run time."

-So how the compiler can know at compile time? -> It can't find the fitting byte size, too.

So what is the usage of these types?

Jens
  • 69,818
  • 15
  • 125
  • 179
dhein
  • 6,431
  • 4
  • 42
  • 74

2 Answers2

5

So why take something that could sometimes cover even more as I need?

Because there might not always be the size you need. For example, on system where CHAR_BIT > 8, int8_t is not available, but int_least8_t is.

Idea is not that compiler will guess how much bits you need. Idea is that compiler will always have type available which will satisfy your size requirement, even if it cannot offer exact size type.

user694733
  • 15,208
  • 2
  • 42
  • 68
  • Would there be in that case for example a uint10_t if charbit would be 10?So it is just for portability purpose, that I don't need to check value of CHAR_BIT by my self and dont have to do coresponding iofdefs? and are there systems in practice where CHAR_BIT is > 8 ? – dhein Jan 27 '15 at 13:39
  • 3
    All `intN_t` types are optional on non 2's complement systems. So they are not really useful for portable code. But `uint10_t` is allowed by standard (N1570: 7.20.1.1.2). I believe there is systems where CHAR_BIT > 8, but I cannot remember how relevant they are today. – user694733 Jan 27 '15 at 13:43
  • 1
    Nowadays systems with CHAR_BIT > 8 are mainly DSPs http://stackoverflow.com/questions/2098149/what-platforms-have-something-other-than-8-bit-char?lq=1 – phuclv Jan 27 '15 at 16:38
5

Because your compiler knows best what is good for you. For example, on some CPU architectures, computations involving 8 or 16 bit types might be much slower than computations done in 32 bits due to extra instructions for masking operands and results to match their width.

The C implementation on a Cray Unicos for example has only an 8 bit char type, everything else (short, int, long, long long) is 64 bit. If you force a type to be int16_t or int32_t performance can suffer drastically due to the narrow stores requiring masking, oring and anding. Using int_least32_t would allow the compiler to use the native 64 bit type.

Jens
  • 69,818
  • 15
  • 125
  • 179
  • Upvote for edit. I didn't realize `intN_t` types could indeed be emulated in software if not otherwise available. – user694733 Jan 27 '15 at 13:48
  • 4
    @Jens and why I should use int_least32_t instead of int_fast32_t for that purpose? – dhein Jan 27 '15 at 13:51
  • @Zaibis Because space efficiency instead of speed might be your priority. – Jens Jan 27 '15 at 14:29
  • @Jens: But you are argumenting for speed efficency, or did I get you wrong? If space efficency might be my priority, as stated in my OP, I would simply use the intN_t variants. Sorry if I got you wrong. – dhein Jan 27 '15 at 14:31
  • 1
    But there might not exist an int32_t; only the least and fast types are required. I should have been more clear: use the fast types when speed is your priority, use the least when space is. – Jens Jan 27 '15 at 16:22
  • indeed it was mentioned in [this question](http://stackoverflow.com/questions/15794124/how-to-use-c99-standard-types-for-maximum-portability-and-efficiency-across-most?rq=1) that "least" types are used when memory usage is more a concern like `uint_least8_t array[100];` and "fast" types are for when speed has higher priority – phuclv Jan 28 '15 at 04:30
  • @LưuVĩnhPhúc So this are generalized 2 oposite options for the case the ballanced type is not available? – dhein Jan 28 '15 at 06:47
  • @Jens: For what sizes would a compiler be required to implement "least" and "fast" types? Is a compiler required to implement `uint_least1_t`, `uint_least2_t`, `uint_least3_t`, etc. all the way up to `uint_least64_t`? – supercat Mar 11 '16 at 22:49