1

I am now going through avr library in "Arduino\hardware\tools\avr\avr\include" folder. In stdint.h file there is the piece of code:

typedef unsigned int uint16_t __attribute__ ((__mode__ (__HI__)));
typedef signed int int32_t __attribute__ ((__mode__ (__SI__)));

typedef uint16_t uint_fast16_t;

/** \ingroup avr_stdint
    fastest signed int with at least 32 bits. */

typedef int32_t int_fast32_t;

So basically int32_t, int_fast32_t and signed int __attribute__ ((__mode__ (__SI__))) are the same thing. Could anybody confirm that?

If yes, why is it done in such way? Why don't just use int32_t?

fuz
  • 88,405
  • 25
  • 200
  • 352
Qeeet
  • 303
  • 3
  • 13
  • 1
    "peace of code" Usually, I don't fuss about language mistakes but this is actually funny. It's "piece," BTW. No offense. :-) – cadaniluk Feb 15 '16 at 20:56
  • 1
    If you're asking why `stdint.h` declares `int_(least|fast)N_t` types as well as the `intN_t` types that you expect, the answer is that the language standard (C99) requires the least/fast types, because the committee thought they would be useful. It turns out they were wrong about that, but backward compatibility means the header has to continue declaring them even though almost nobody uses them. If you're asking something else, then I don't understand what you are asking, please clarify. – zwol Feb 15 '16 at 20:57
  • @cad. thanks :), just mechanical mark – Qeeet Feb 15 '16 at 20:58
  • @zwol, that is what I wanted to hear. Now I understand. Thank you. Put that into the answer – Qeeet Feb 15 '16 at 20:59
  • 2
    `int_(least|fast)N_t types` are somewhat useful on (increasing rare) platforms where the `int` width is not a power-of-2. – chux - Reinstate Monica Feb 15 '16 at 21:21

2 Answers2

2

The actual answer depends on your implementation. The actual point for the typedefs is to give programmers who have to care about micro-optimisations like choosing one integer type over another, just because of the slight performance gains, the possibility to still write platform independent code. signed int __attribute__ ((__mode__(__SI__))) may be the best performing integer type on one platform, but as soon as one decides to support another platform, there would be thousand of types, which have to be changed.

Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
Vincent
  • 156
  • 1
  • 9
2

I understand the question to be "Why does stdint.h declare types with names like int_leastN_t and int_fastN_t as well as the intN_t that I expect it to declare?"

The simple answer is that the C standard (since its 1999 revision) requires stdint.h to declare those types, because the committee thought they would be useful. As it happens, they were wrong; almost nobody wants anything out of stdint.h but the exact-width types. But it is very, very rare for anything to get removed from the C standard once it's been included, because that would break programs that are using them. So stdint.h will probably continue to declare these types forever.

(I could go on at considerable length about why non-exact-width types are less than useful in C, but you probably don't care.)

zwol
  • 135,547
  • 38
  • 252
  • 361
  • Agree "wants anything out of stdint.h but the exact-width types.". I ponder a future C model that [primarily uses fixed width types](http://stackoverflow.com/q/30942107/2410359) and fixes `int/unsigned` (the rank of automatic promotion) to 32-bit, even when machine width grows to 128. – chux - Reinstate Monica Feb 15 '16 at 21:18
  • @chux: No problem capping `int` etc. But not fix them to 32 bits. 16 bits is fine. In general it would be better just to make the standard types like `fast, etc. (oops, they already are ...). – too honest for this site Feb 15 '16 at 21:50
  • Reason these types are not commonly used is likely just lazy programmers (like me) and redundancy, as the standard types actually are much the same. – too honest for this site Feb 15 '16 at 21:51
  • @Olaf If you're trying to bait me into going on at considerable length, I suggest you ask a new question instead. I don't really have time for it, but it might attract answers from some of the other C veterans ;-) – zwol Feb 15 '16 at 23:16