ANSI-C Standard Types size_t, wchar_t, and ptrdiff_t Defines

ANSI provides some standard defines in stddef.h to deal with the implementation of defined object sizes.

The following listing shows part of the contents of stdtypes.h (included from stddef.h).

Listing: Type Definitions of ANSI-C Standard Types
/* size_t: defines the maximum object size type */
  #if defined(__SIZE_T_IS_UCHAR__)

    typedef unsigned char  size_t;

  #elif defined(__SIZE_T_IS_USHORT__)

    typedef unsigned short size_t;

  #elif defined(__SIZE_T_IS_UINT__)

    typedef unsigned int   size_t;

  #elif defined(__SIZE_T_IS_ULONG__)

    typedef unsigned long  size_t;

  #else

    #error "illegal size_t type"

  #endif

/* ptrdiff_t: defines the maximum pointer difference type */

  #if defined(__PTRDIFF_T_IS_CHAR__)

    typedef signed char   ptrdiff_t;

  #elif defined(__PTRDIFF_T_IS_SHORT__)

    typedef signed short  ptrdiff_t;

  #elif defined(__PTRDIFF_T_IS_INT__)

    typedef signed int    ptrdiff_t;

  #elif defined(__PTRDIFF_T_IS_LONG__)

    typedef signed long   ptrdiff_t;

  #else

    #error "illegal ptrdiff_t type"

  #endif

/* wchar_t: defines the type of wide character */

#if defined(__WCHAR_T_IS_UCHAR__)

  typedef unsigned char  wchar_t;

#elif defined(__WCHAR_T_IS_USHORT__)

  typedef unsigned short wchar_t;

#elif defined(__WCHAR_T_IS_UINT__)

  typedef unsigned int   wchar_t;

#elif defined(__WCHAR_T_IS_ULONG__)

  typedef unsigned long  wchar_t;

#else

  #error "illegal wchar_t type"

#endif

The following table lists define s that deal with other possible implementations:

Table 1. Defines for Other Implementations
Macro Description
__SIZE_T_IS_UCHAR__ Defined if the Compiler expects size_t in stddef.h to be unsigned char.
__SIZE_T_IS_USHORT__ Defined if the Compiler expects size_t in stddef.h to be unsigned short.
__SIZE_T_IS_UINT__ Defined if the Compiler expects size_t in stddef.h to be unsigned int.
__SIZE_T_IS_ULONG__ Defined if the Compiler expects size_t in stddef.h to be unsigned long.
__WCHAR_T_IS_UCHAR__ Defined if the Compiler expects wchar_t in stddef.h to be unsignedchar.
__WCHAR_T_IS_USHORT__ Defined if the Compiler expects wchar_t in stddef.h to be unsignedshort.
__WCHAR_T_IS_UINT__ Defined if the Compiler expects wchar_t in stddef.h to be unsignedint.
__WCHAR_T_IS_ULONG__ Defined if the Compiler expects wchar_t in stddef.h to be unsignedlong.
__PTRDIFF_T_IS_CHAR__ Defined if the Compiler expects ptrdiff_t in stddef.h to be char.
__PTRDIFF_T_IS_SHORT__ Defined if the Compiler expects ptrdiff_t in stddef.h to be short.
__PTRDIFF_T_IS_INT__ Defined if the Compiler expects ptrdiff_t in stddef.h to be int.
__PTRDIFF_T_IS_LONG__ Defined if the Compiler expects ptrdiff_t in stddef.h to be long.

The following tables show the default settings of the ANSI-C Compiler standard types size_t and ptrdiff_t.

This section covers the following topics: