How can I stringify a macro using a macro?

135 Views Asked by At
#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

There are 1 best solutions below

0
Angew is no longer proud of SO On

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]