Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to print a preprocessor variable in C?

Is is possible to print to stderr the value of a preprocessor variable in C? For example, what I have right now is:

#define PP_VAR (10) #if (PP_VAR > 10)     #warning PP_VAR is greater than 10 #endif 

But what I'd like to do is:

#define PP_VAR (10) #if (PP_VAR > 10)     #warning PP_VAR=%PP_VAR% #endif 

Is something like this possible in C?

like image 314
apalopohapa Avatar asked Jul 30 '09 02:07

apalopohapa


People also ask

How do you print #define value in C?

printf("%d", 5);

What is a preprocessor variable?

A preprocessor variable is specified in a %DECLARE statement with the FIXED, CHARACTER, or INITIAL attribute. No other attributes can be declared for a preprocessor variable, and attributes must not be repeated. (Other attributes are supplied by the preprocessor, however.)

What is C preprocessor output?

When the C preprocessor is used with the C, C++, or Objective-C compilers, it is integrated into the compiler and communicates a stream of binary tokens directly to the compiler's parser. However, it can also be used in the more conventional standalone mode, where it produces textual output.

What is the preprocessor statement in C?

The C preprocessor is a macro preprocessor (allows you to define macros) that transforms your program before it is compiled. These transformations can be the inclusion of header files, macro expansions, etc.


2 Answers

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.

like image 116
MattM Avatar answered Sep 23 '22 02:09

MattM


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 
like image 43
Marc Abramowitz Avatar answered Sep 25 '22 02:09

Marc Abramowitz