|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On May 9, 10:26pm, "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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On May 10, 12:26 am, "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"; > Question 11.17 of the C-faq. <http://c-faq.com/> |
|
![]() |
| Outils de la discussion | |
|
|