470,833 Members | 1,722 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

Quoting symbol in CPP

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
Jun 27 '08 #1
2 1236
On May 9, 10:26*pm, "Paul J. Lucas" <paul-nos...@nospam.lucasmail.org>
wrote:
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
Jun 27 '08 #2
On May 10, 12:26 am, "Paul J. Lucas" <paul-
nos...@nospam.lucasmail.orgwrote:
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/>
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Richard | last post: by
22 posts views Thread by Jane Withnolastname | last post: by
27 posts views Thread by Ron Adam | last post: by
1 post views Thread by vsp15584 | last post: by
6 posts views Thread by sadegh | last post: by
7 posts views Thread by Steven W. Orr | last post: by
reply views Thread by Ryan Gaffuri | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.