4

I have the following problem:

extern void func_name(const char *f);  
#define EXPECT(f) func_name(#f)  
#define foo bar  

void main()  
{  
     EXPECT(foo);  
}  

So, the

EXPECT(foo);

will be actually evaluated to

func_name("foo");

What I actually need is to convert 'foo' to 'bar', i.e. the code to become

func_name("bar");

So I kind of want to do this:

#define "foo" "bar"

But this does not work, because macro names must be identifiers. I also tried to find a way to change the preprocessor precedence, so my macro will be replaced first, but didn't find a way.

The perfect solution will not change main() at all.

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Arik
  • 1,257
  • 9
  • 13
  • 2
    I just ran into this. http://stackoverflow.com/questions/25331999/macro-meta-programming – IdeaHat Aug 20 '14 at 20:57
  • 3
    If your textbook tells you that `void main()` is correct, its author has lied to you. Get a better book. The correct definition is `int main(void)`. – Keith Thompson Aug 20 '14 at 21:01
  • possible duplicate of [C Preprocessor, Stringify the result of a macro](http://stackoverflow.com/questions/3419332/c-preprocessor-stringify-the-result-of-a-macro) – Alex Celeste Aug 20 '14 at 22:38

1 Answers1

4
#define S(x) #x
#define EXPECT(f) func_name(S(f))  
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70