header image
[ # ] INT*_C Macros
September 11th, 2012 under Programming

The header

1
stdint.h
stdint.h
defines loads of macros. As such it is an omnipresent header that is included in many other headers. Being a C99 header, some macros are disabled in C++. One example are the
1
INT8_C, INT16_C, INT32_c and INT64_C
INT8_C, INT16_C, INT32_c and INT64_C
macros. When looking at the header (see below) one sees that the macros can be enabled in C++ by defining
1
__STDC_CONSTANT_MACROS
__STDC_CONSTANT_MACROS
.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* The ISO C99 standard specifies that in C++ implementations these
   should only be defined if explicitly requested.  */
#if !defined __cplusplus || defined __STDC_CONSTANT_MACROS
 
/* Signed.  */
# define INT8_C(c)      c
# define INT16_C(c)     c
# define INT32_C(c)     c
# if __WORDSIZE == 64
#  define INT64_C(c)    c ## L
# else
#  define INT64_C(c)    c ## LL
# endif
 
...
#endif
/* The ISO C99 standard specifies that in C++ implementations these
   should only be defined if explicitly requested.  */
#if !defined __cplusplus || defined __STDC_CONSTANT_MACROS

/* Signed.  */
# define INT8_C(c)      c
# define INT16_C(c)     c
# define INT32_C(c)     c
# if __WORDSIZE == 64
#  define INT64_C(c)    c ## L
# else
#  define INT64_C(c)    c ## LL
# endif

...
#endif

When i tried this in some C++ file in my project, it didn’t fix the

1
error: ‘UINT64_C’ was not declared in this scope
error: ‘UINT64_C’ was not declared in this scope
. I realized that the header has already been included before I defined
1
__STDC_CONSTANT_MACROS
__STDC_CONSTANT_MACROS
. Due the the header guard the define has no effect. To fix this problem I simply undef the header guard.

1
2
3
4
5
#define __STDC_CONSTANT_MACROS
#ifdef _STDINT_H
# undef _STDINT_H
#endif
# include <stdint.h>
#define __STDC_CONSTANT_MACROS
#ifdef _STDINT_H
# undef _STDINT_H
#endif
# include <stdint.h>

Of course this trick does not work for all header but in the case of

1
stdint.h
stdint.h
it works because this header only defines macros.