Is it possible to print a preprocessor variable in C? Is it possible to print a preprocessor variable in C? c c

Is it possible to print a preprocessor variable in C?


You can print out the value of a preprocessor variable under visual studio. The following prints out the value of _MSC_VER:

#define STRING2(x) #x#define STRING(x) STRING2(x)#pragma message(STRING(_MSC_VER))

Not sure how standard this is though.


This works with GCC 4.4.3:

#define STRING2(x) #x#define STRING(x) STRING2(x)#pragma message "LIBMEMCACHED_VERSION_HEX = " STRING(LIBMEMCACHED_VERSION_HEX)

yields:

src/_pylibmcmodule.c:1843: note: #pragma message: LIBMEMCACHED_VERSION_HEX = 0x01000017


Many C compilers support #warning (but it is not defined by the C standard).

However, GCC at least does not do pre-processing on the data that follows, which means it is hard to see the value of a variable.

#define PP_VAR 123#warning "Value of PP_VAR = " PP_VAR#warning "Value of PP_VAR = " #PP_VAR#warning "Value of PP_VAR = " ##PP_VAR

GCC produces:

x.c:2:2: warning: #warning "Value of PP_VAR = " PP_VARx.c:3:2: warning: #warning "Value of PP_VAR = " #PP_VARx.c:4:2: warning: #warning "Value of PP_VAR = " ##PP_VAR