Relying on the compiler vendor's implementation of primitive type sizes
WILL come back to haunt you if you ever compile your code on another
machine architecture, OS, or another vendor's compiler .
Most compiler vendors provide a header file that defines primitive types with
explict type sizes.
These primitive types should be used when ever code may be potentially ported
to another compiler (read this as ALWAYS in EVERY instance).
For example, most UNIX compilers have int8_t uint8_t int16_t int32_t uint32_t
.
Microsoft has INT8 UINT8 INT16 UINT16 INT32 UINT32
.
I prefer Borland/CodeGear's int8 uint8 int16 uint16 int32 uint32
.
These names also give a little reminder of the size/range of the intended value.
For years I have used Borland's explicit primitive type names
and #include
the following C/C++ header file (primitive.h)
which is intended to define the explicit primitive types with these names for any C/C++ compiler
(this header file might not actually cover every compiler but it covers several compilers I have used on Windows, UNIX and Linux, it also doesn't (yet) define 64bit types).
#ifndef primitiveH
#define primitiveH
// Header file primitive.h
// Primitive types
// For C and/or C++
// This header file is intended to define a set of primitive types
// that will always be the same number bytes on any operating operating systems
// and/or for several popular C/C++ compiler vendors.
// Currently the type definitions cover:
// Windows (16 or 32 bit)
// Linux
// UNIX (HP/US, Solaris)
// And the following compiler vendors
// Microsoft, Borland/Imprise/CodeGear, SunStudio, HP/UX
// (maybe GNU C/C++)
// This does not currently include 64bit primitives.
#define float64 double
#define float32 float
// Some old C++ compilers didn't have bool type
// If your compiler does not have bool then add emulate_bool
// to your command line -D option or defined macros.
#ifdef emulate_bool
# ifdef TVISION
# define bool int
# define true 1
# define false 0
# else
# ifdef __BCPLUSPLUS__
//BC++ bool type not available until 5.0
# define BI_NO_BOOL
# include <classlib/defs.h>
# else
# define bool int
# define true 1
# define false 0
# endif
# endif
#endif
#ifdef __BCPLUSPLUS__
# include <systypes.h>
#else
# ifdef unix
# ifdef hpux
# include <sys/_inttypes.h>
# endif
# ifdef sun
# include <sys/int_types.h>
# endif
# ifdef linux
# include <idna.h>
# endif
# define int8 int8_t
# define uint8 uint8_t
# define int16 int16_t
# define int32 int32_t
# define uint16 uint16_t
# define uint32 uint32_t
# else
# ifdef _MSC_VER
# include <BaseTSD.h>
# define int8 INT8
# define uint8 UINT8
# define int16 INT16
# define int32 INT32
# define uint16 UINT16
# define uint32 UINT32
# else
# ifndef OWL6
// OWL version 6 already defines these types
# define int8 char
# define uint8 unsigned char
# ifdef __WIN32_
# define int16 short int
# define int32 long
# define uint16 unsigned short int
# define uint32 unsigned long
# else
# define int16 int
# define int32 long
# define uint16 unsigned int
# define uint32 unsigned long
# endif
# endif
# endif
# endif
#endif
typedef int8 sint8;
typedef int16 sint16;
typedef int32 sint32;
typedef uint8 nat8;
typedef uint16 nat16;
typedef uint32 nat32;
typedef const char * cASCIIz; // constant null terminated char array
typedef char * ASCIIz; // null terminated char array
#endif
//primitive.h
#include <stdint.h>
and then the types which tell the size. E.g.uint32_t
. On a new platform, then you only need to make sure stdint.h gets it right for that particular platform and your code works as intended. – Fertilizer