3

I would like char, short and int types to be 1, 2 and 4 bytes width. I have included a stdint.h header into my sources. Does this guarantee that int8_t, int16_t and int32_t integer types will be of widths specified? What is the best way to achieve this?

ezpresso
  • 7,896
  • 13
  • 62
  • 94
  • This is OS/Architecture defined. For a 32 bit machine they should have that size. – BlackBear Feb 04 '11 at 21:04
  • Yes, but I read here that a lot of compilers do not support stdint.h. – ezpresso Feb 04 '11 at 21:11
  • It can sometimes also depend on the compiler (and the flags being used). What OS/CPU/compiler are you working with? – Julio Gorgé Feb 04 '11 at 21:11
  • Ubuntu 10.10 / Core 2 Quad / GCC 4.4 – ezpresso Feb 04 '11 at 21:15
  • 2
    is a C99 header, and will not be available in older compilers. Most significantly it was not included in VC++ until the 2010 version. However a usable implementation can be obtained from http://msinttypes.googlecode.com/svn/trunk/stdint.h Since it ie entirely a header implmentation, you can create your own for any target/compiler you might use; the compiler's documentation will specify the sizes of all built-in types. – Clifford Feb 04 '11 at 21:39

4 Answers4

7

If these types exist, they have the correct width and are encoded in two's complement.

Edit: you should be able to check if the types exist with something like

#ifdef INT32_MAX
...
#endif

but on all end user architectures (PC etc) these types do usually exist.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • 2
    @Conrad - Only if they're not avaliable. If they exist they have to be there. – Chris Lutz Feb 05 '11 at 09:00
  • Right. But still, they aren't guaranteed to exist (one might imagine a reasonable architecture only allowing integer operations / loads and stores on 64-bit and 8-bit values). – Conrad Meyer Feb 05 '11 at 18:38
4

The whole purpose of the types defined in stdint.h is of them to be of specified width. So int8_t will be 8 bits wide, int16_t - 16 bits wide, etc.

Nikolai Fetissov
  • 82,306
  • 11
  • 110
  • 171
1

stdint.h some exact-width integer types. These are (from wikipedia):

Specifier   Signing      Bits   Bytes   
int8_t        Signed       8       1    
uint8_t       Unsigned     8       1    
int16_t       Signed       16      2    
uint16_t      Unsigned     16      2    
int32_t       Signed       32      4    
uint32_t      Unsigned     32      4    
int64_t       Signed       64      8 
uint64_t      Unsigned     64      8    

Use them if you want to know what the size is for definite. C99 requires that these types have the sizes specified. All other types are platform dependent.

Other types are guaranteed to be a certain minimum width, as per limits.h. But they could equally be bigger than that. It is up to the implementation.

1

C99's stdint.h does not actually guarantee that these types exist. To quote wikipedia:

These types are optional unless the implementation supports types with widths of 8, 16, 32 or 64, then it shall typedef them to the corresponding types with corresponding N.

However, if they do exist, they will be the correct widths.

Conrad Meyer
  • 2,851
  • 21
  • 24
  • 3
    Actually, the standard seems to make it optional to define the signed types if the implementation doesn't use two's complement representation. 7.18.1.1 Exact-width integer types ¶ 3: "These types are optional. However, if an implementation provides integer types with widths of 8, 16, 32, or 64 bits, no padding bits, and (for the signed types) that have a two's complement representation, it shall define the corresponding typedef names." – Chris Lutz Feb 05 '11 at 09:04