Ich bin momentan dabei, eine Netzwerkanwendung zu schreiben, die dementsprechend überall gleichgroße Datentypen verwenden sollte. In C99 gibts zwar den Header stdint.h, in dem int8_t und Co definiert sind, aber offensichtlich scheinen den selbst nach 10 Jahren noch nicht alle Compiler zu kennen. Zumindest sagt mir das aktulle MS Visual Studio, dass es weder stdint.h, noch cstdint kennt. Mein aktueller Ansatz war folgender:

Code (c++):
#ifndef STDINT_H
#define STDINT_H
 
#include <climits>
 
// Definition of 8 bit types
#if UCHAR_MAX >= 255
typedef unsigned char uint8_t;
typedef signed char int8_t;
#elif USHRT_MAX >= 255
typedef unsigned short uint8_t;
typedef signed short int8_t;
#elif UINT_MAX >= 255
typedef unsigned int uint8_t;
typedef signed int int8_t;
#elif ULONG_MAX >= 255
typedef unsigned long uint8_t;
typedef signed long int8_t;
#endif
 
// Definition of 16 bit types
#if UCHAR_MAX >= 65535
typedef unsigned char uint16_t;
typedef signed char int16_t;
#elif USHRT_MAX >= 65535
typedef unsigned short uint16_t;
typedef signed short int16_t;
#elif UINT_MAX >= 65535
typedef unsigned int uint16_t;
typedef signed int int16_t;
#elif ULONG_MAX >= 65535
typedef unsigned long uint16_t;
typedef signed long int16_t;
#endif
 
// Definition of 32 bit types
#if UCHAR_MAX >= 4294967295
typedef unsigned char uint32_t;
typedef signed char int32_t;
#elif USHRT_MAX >= 4294967295
typedef unsigned short uint32_t;
typedef signed short int32_t;
#elif UINT_MAX >= 4294967295
typedef unsigned int uint32_t;
typedef signed int int32_t;
#elif ULONG_MAX >= 4294967295
typedef unsigned long uint32_t;
typedef signed long int32_t;
#endif
 
#endif STDINT_H


Nicht elegant, funktioniert nur auf Prozessoren, bei denen 1 Byte = 8 Bit richtig und garantiert nicht, dass man exakt die Größe hat, die man will, sondern nur, dass man den kleinstmöglichen Datentyp hat. Insgesamt unbefriedigend.