mikeage.net Logo
mikeage.net/2006/10/13/preprocessor-s-s-and-other-weird-stuff/

mikeage.net @ ו׳ טבת תשע״ט

Preprocessor #s, ##s, and other weird stuff

In the post on strings for enums , I used two types of pound symbols in the preprocessor macros: # and ##. Here's some more detail:

The first preprocessor macro is ##, which means to concatenate what comes after it with what was before. For example:

#define FOO(x) BAR##x
FOO(f)
FOO(test)

will produce (not as legal C, of course):

BARf
BARtest

The second macro is #. That will produce a quoted string from the given argument:

#define FOO(x) #x
FOO(bar)

produces:

"bar"

Note that you cannot use # on non variables. Thus, the following is illegal:

#define FOO(x) #TEST x

If you want to quote the generated output of a macro, it has to be done in stages:

#define STRINGIFY(x) #x
#define FOO(x) STRINGIFY(TEST##x)

FOO(bar)

will produce

"TESTbar"

Enjoy

One Response to “Preprocessor #s, ##s, and other weird stuff”

  1. […] the use of a nested #define. I'll be writing another post to explain a little more about that […]

Leave a Reply

Quick Map
Content +
Personal +
Archives +
Site Stuff +
RBS Weather +
Search +
Recent Images

Valid XHTML 1.1!
Printer Friendly Page
 

Last Modified: September 04, 2006 @ 02:11 CST

Memory(TRUE): 2097152/2097152
Memory(FALSE): 3275448/3285400