4
#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.

14
  • 1
    why a string ? Why not TRACE(Hello) ? Commented Jun 21, 2018 at 7:05
  • yAlso, here your TRACE has exactly one macro argument: arg1 is bound to ("Hello",a). Commented Jun 21, 2018 at 7:12
  • since TRACE(Hello) is used in another macro am using as TRACE(("Hello")) and more over , can use printf function. So I'm retaining it. Commented Jun 21, 2018 at 7:19
  • what do you mean by "used in another macro" ? What do you mean by "can use printf function" ? Can you show code for both to clarify what you want to achieve and what your restrictions are ? Commented Jun 21, 2018 at 7:55
  • #define SMM_TRACE(x) printf x #define SMM_TRACE_CONVERT SMM_TRACE(CONVERT_ERROR_KEY_FAILED) int main() { int num = 1; SMM_TRACE(("the number is %d\n", num)); printf("%s","SMM_TRACE_CONVERT\n"); return 0; } Commented Jun 21, 2018 at 9:42

2 Answers 2

7

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".

Sign up to request clarification or add additional context in comments.

3 Comments

ok I will use the token hello and I ll stringify it when needed. another problem is the inner parenthesis (). With parentheses (hello) will not be a variable. How to avoid this. is there any way to eliminate this.
@w.jayson You should search for "c variadic macros" if that is what you need. They are part of the C language starting with C99. See, e.g., en.wikipedia.org/wiki/Variadic_macro
Yes, am trying to implement variadic macros. The only thing I dint get is, is it possible to use #define TRACE((arg1,...)) instead of #define TRACE(arg1,...) so that I can easily avoid the restrictions.
0

Thank you all of you for spending your valuable time to respond my question. Some of your comments gave me an idea to sort out the answer. you can find the answer below :

#define TRACE(arg1,...)  TRACE2 arg1
#define TRACE2(arg1, arg2) static const char arg1; \
                             printf("%p\n",(void*)&arg1);\
                              printf("%d\n",arg2);\

If any changes can be done in this code, kindly let me know.

1 Comment

@Jens: if the invocation of TRACE has double parentheses as in the question, this is OK.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.