14

On the MSVC++ compiler, one can use the __int8, __int16, __int32 and similar types for integers with specific sizes. This is extremely useful for applications which need to work with low-level data structures like custom file formats, hardware control data structures and the like.

Is there a similar equivalent I can use on the GCC compiler?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Pramod
  • 9,256
  • 4
  • 26
  • 27

1 Answers1

33

ISO standard C, starting with the C99 standard, adds the standard header <stdint.h> that defines these:

uint8_t  - unsigned 8 bit
int8_t   - signed 8 bit
uint16_t - unsigned 16 bit
int16_t  - signed 16 bit
uint32_t - unsigned 32 bit
int32_t  - signed 32 bit
uint64_t - unsigned 64 bit
int64_t  - signed 64 bit

I use these types all the time.

These types are defined only if the implementation supports predefined types with the appropriate sizes and characteristics (which most do).

<stdint.h> also defines types with names of the form (u)int_leastN_t (types that have at least the specified width) and (u)int_fastN_t (the "fastest" types that have at least the specified width); these types are mandatory.

If you're using an old implementation that doesn't support <stdint.h>, you can roll your own; one implementation is Doug Gwyn's "q8".

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Jason Coco
  • 77,985
  • 20
  • 184
  • 180
  • 3
    stdint.h is also part of C99, so it's no longer posix-specific. – puetzk Nov 06 '08 at 18:01
  • I see. I vaguely remember seeing a compiler attribute that does something similar.... – Pramod Nov 06 '08 at 18:04
  • I remember having this problem the other way around many years ago (not uint8_t on Microsoft) :) – Robert Gould Nov 06 '08 at 18:56
  • @Pramod: it exists, but you do not want to use it. It is __attribute__((mode(...))) – CesarB Nov 07 '08 at 00:20
  • 1
    They're only required in ISO if the implementation has the types to support them (which should be most implementations, I know). There's also the `atleast` variants as well for types that are that size or greater. – paxdiablo Jan 20 '11 at 06:20
  • @paxdiablo: very good point, but to be quite frank, if you're compiling for architectures that don't support signed and unsigned variants at the above widths, you're most certainly *not* asking this question ;) – Jason Coco Jan 20 '11 at 06:25