Quoting symbol in CPP

P

Paul J. Lucas

Given this:

#define NUM 100

I was to use a CPP macro like:

char *p = MACRO(NUM);

and have MACRO defined such that it would expand to:

char *p = "100";

I tried:

#define MACRO(X) #X

but, for MACRO(NUM), that expands to:

char *p = "NUM";

How can I get CPP to expand NUM first?

- Paul
 
S

Szabolcs Borsanyi

Given this:

        #define NUM 100

I was to use a CPP macro like:

        char *p = MACRO(NUM);

and have MACRO defined such that it would expand to:

        char *p = "100";

I tried:

        #define MACRO(X) #X

but, for MACRO(NUM), that expands to:

        char *p = "NUM";

How can I get CPP to expand NUM first?

- Paul

The preprocessor will expand the macro parameters first, unless
they appear next to a # (or ##).
So try this:

#define NUM 100
#define MACRO(X) #X
#define GOODMACRO(X) MACRO(X)

char *p=GOODMACRO(NUM);

GOODMACRO's argument is not following an #, so it is expanded, prior
to
looking at MACRO's meaning.

Szabolcs
 
V

vippstar

Given this:

#define NUM 100

I was to use a CPP macro like:

char *p = MACRO(NUM);

and have MACRO defined such that it would expand to:

char *p = "100";

I tried:

#define MACRO(X) #X

but, for MACRO(NUM), that expands to:

char *p = "NUM";
Question 11.17 of the C-faq.
<http://c-faq.com/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,529
Latest member
JaclynShum

Latest Threads

Top