111

I often see source code using types like uint32, uint64 and I wonder if they should be defined by the programmer in the application code or if they are defined in a standard lib header.

What's the best way to have these types on my application source code?

user5534993
  • 518
  • 2
  • 17
felipecrv
  • 1,780
  • 2
  • 15
  • 14

4 Answers4

168

The C99 stdint.h defines these:

  • int8_t
  • int16_t
  • int32_t
  • uint8_t
  • uint16_t
  • uint32_t

And, if the architecture supports them:

  • int64_t
  • uint64_t

There are various other integer typedefs in stdint.h as well.

If you're stuck without a C99 environment then you should probably supply your own typedefs and use the C99 ones anyway.

The uint32 and uint64 (i.e. without the _t suffix) are probably application specific.

m-ric
  • 5,621
  • 7
  • 38
  • 51
mu is too short
  • 426,620
  • 70
  • 833
  • 800
15

Those integer types are all defined in stdint.h

GWW
  • 43,129
  • 11
  • 115
  • 108
  • Isn't `stdint.h` implementation-specific? Also see [this question](http://stackoverflow.com/questions/911035/uint32-int16-and-the-like-are-they-standard-c) and [this question](http://stackoverflow.com/questions/734802/fixed-width-integers-in-c) – Chris Frederick May 16 '11 at 04:49
  • 3
    Looking at the file I found "ISO C99: 7.18 Integer types " in the comments. I suppose you get `stdint.h` if you're respecting the C99 standard. – felipecrv May 16 '11 at 04:53
  • I'm not finding any of them in `stdint.h`. This returns 0 hits: `grep uint32 /usr/lib/gcc/x86_64-linux-gnu/4.9/include/stdint.h | grep -v uint32_t`. – jww Jul 30 '16 at 20:57
  • 1
    @jww: In my case, that file contains `#include "stdint-gcc.h"`, and grepping *that* file yields the definitions. – Victor Zamanian Aug 16 '16 at 16:57
  • @VictorZamanian - I'm still not finding them `stdint-gcc.h`. There is a `typedef __UINT32_TYPE__ uint32_t`; but nothing for `uint32`. The same applies to `uint8`, `uint16` and `uint64` as well (and the signed variants). – jww Aug 16 '16 at 22:14
  • 1
    @jww yeah, they don't exist. The only ones that are standard and that should be defined there are the ones with the `_t` suffix, as listed in the answer above. – Victor Zamanian Aug 16 '16 at 23:43
2

If you are using C99 just include stdint.h. BTW, the 64bit types are there iff the processor supports them.

BiGYaN
  • 6,974
  • 5
  • 30
  • 43
  • Please cite something in this regard. – BiGYaN May 16 '11 at 16:41
  • 6
    x86 does not support 64-bit math but has 64-bit types. – R.. GitHub STOP HELPING ICE May 16 '11 at 17:16
  • 2
    Yes it does. It supports 32-bit x 32-bit -> 64-bit multiplication, 64-bit / 32-bit -> 32-bit quotient & 32-bit remainder division, as well as having add with carry and subtract with borrow instructions. It was built to be able to perform 64-bit arithmetic, if only because the 8086 needed to be able to do 32-bit arithmetic. – Michael Morris Apr 08 '14 at 02:47
  • There is also a `uint128_t` if `__SIZEOF_INT128__` is defined to 16 or greater (GCC and compatibles). Its available on x86_64/amd64 machines, but the processor does not natively support it. Only Cray's have that register size, IIRC. – jww Aug 16 '16 at 22:14
0

The questioner actually asked about int16 (etc) rather than (ugly) int16_t (etc).

There are no standard headers - nor any in Linux's /usr/include/ folder that define them without the "_t".

Steve
  • 21
  • 3