Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I stringify a macro using a macro?

#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?

like image 561
user190341309 Avatar asked Jan 27 '26 10:01

user190341309


1 Answers

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]

like image 145
Angew is no longer proud of SO Avatar answered Jan 30 '26 01:01

Angew is no longer proud of SO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!