What´s the use of minimum-width integer types ?

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

While reading the avr-libc 1.4.4 documentation I stumbled across the definition of "Minimum-width integer types" such as int_least8_t, uint_least8_t, etc.

What´s the use of such a definition? Assure that enough memory is available? But then I´d use a "full" int8_t anyway.

What other reasons exist for such a definition?

Ingo

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

> What's the use of such a definition?

Well, the rationale of the C99 standard isn't very explicit about why
they have been added. The only sentence there is:

``It was observed that macros for minimum and maximum limits for other
integer typedefs in standard headers would be similarly useful, so
these were added.''

My only guess is that if a portable application doesn't really care if
some environment assigns more bits than minimally needed, you could
consider using them. Suppose, you'd like to have a counter variable
that counts from 0 to 200, the obvious solution is to use uint8_t.
Now, if you were porting your application to a platform that doesn't
provide 8-bit integers at all (as some DSPs are supposedly working
that way, as they only offer 32-bit integer types), the compilation of
that application will fail. However, as your actual concern wasn't to
have an *exact* 8-bit variable but to have the smallest variable where
your [0..200] range is be covered by, using uint_least8_t would make
that application more portable as it could still compile on that
(admittedly exotic) platform.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That, and perhaps it is designed to allow the environment to select the most efficient data type. Example on some 16-bit CPUs I believe 8-bit data types are available, but they are less efficient than an actual 16 bit value.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Maybe more to the point: Originally C didn't say how many bits go into an "int" (or "char"), so there was no way to write portable programs that care much about bit width without such types. I think eventually some folk decided that allowing an "int" to be 8 bits was not a good idea, but ISTR that it can still be 16 bits in conformant implementations. Normally an "int" matches the machine's word size. And a wchar_t is probably 16 bits ...

Now, contrary to Microsoft, a "word" is a machine dependant size. So a "32 bit machine" uses 32 bit words/ints, a "16 bit machine" uses 16 bit words/ints ... and an 8 bit machine uses 16 bit ints even though that generates worse code, just so that C programs can rely on 16 bit ints, which helps code portability (a bit). And many 64 bit machines use 32 bit ints just so that all the 32 bit code libraries can be quickly ported (which helps portability a LOT, given how much 32 bit code exists).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not that this has anything to do with the OP's actual question about minimum-width integer types,...

mojojojo wrote:
but ISTR that it can still be 16 bits in conformant implementations. Normally an "int" matches the machine's word size.

The ISO/IEC 9899:1990 Standard ("ANSI C") requires that an int should be "the natural size suggested by the architecture", but also requires that it be able to represent unsigned values up to at least 65535 (ie, be at least 16 bits).

This is obviously fine for machines with a word size of at least 16 bits, but is a contradiction for machines with a word size of less than 16 bits - such as the AVR.

This is why C's integer-promotion rules can be problematic on 8-bit machines...!

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...