Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a string to variable name using macro?

Tags:

c

string

macros

#define TRACE(arg1,...)  char* arg1; 

int main(void)
{
    int a=4;
    TRACE(("Hello",a));  // convert "Hello" to a valid char variable name.
    return 0;
}

I'm having trouble in converting the string "Hello" into a variable name. for example: "Hello" should be converted as const char* Hello; by using a macro. Since there are double quotes I'm unable to convert it. This is my first question in Stack Overflow.

like image 775
w.jayson Avatar asked Sep 11 '25 22:09

w.jayson


1 Answers

You can't "destringify" a string in C.

You can stringify a token, though, so the solution is to do it the other way around: use the token hello and stringify it when you need "hello".

like image 173
Jens Avatar answered Sep 16 '25 10:09

Jens