3
#define A_1 {2,1124,124110,2,0,20,5121,0,21241,10} #define SFY(macro) #macro void func(char* s, int i) { printf("%d %s", i, s); } int main (void) { int a[10] = A_1; func(SFY(A_1), 2); } 

My desired output of this would be:

2 {2,1124,124110,2,0,20,5121,0,21241,10} 

How can I stringify a macro like this?

1
  • 1
    Whenever you find yourself with the need for strange things like this, it usually means that your program design has derailed and that you should step back and fix that underlying problem. For example, is there actually a reason why you can't you use a table of const struct. Commented Feb 23, 2018 at 7:39

1 Answer 1

4

You need to add one more layer of indirection in order to expand the macro (and since it contains commas, you must go variadic):

#define A_1 {2,1124,124110,2,0,20,5121,0,21241,10} #define SFY_2(...) #__VA_ARGS__ #define SFY(macro) SFY_2(macro) void func(char* s, int i) { printf("%d %s", i, s); } int main (void) { int a[10] = A_1; func(SFY(A_1), 2); } 

[Live example]

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.