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. mikeage.net » Blog Archive » Creating an array of strings for an enum Posted (PB):

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

Leave a Reply

Verification Image

Please type the letters you see in the picture. This field is case-insensitive, and valid letters are A-Z (no numbers)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Quicktags:

Quick Map
Content +
Personal +
Archives +
Site Stuff +
RBS Weather +
Search +
New Images
Visitors
Clustermap
Blogroll

Valid XHTML 1.1!
Printer Friendly Page
 

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