+ 1

Someone please trace this code because I am not able to understand the syntax itself for this code.

#define macro(r, a, v) v##a##r #define DECVAR macro(r, a, v) int main() { int DECVAR = 42; var++; printf("%d", var); return 0; }

1st May 2020, 2:20 PM
Wolf Master
Wolf Master - avatar
2 Answers
+ 5
The two hashes (##) symbol appearing in macro definitions is the token-pasting operator. https://docs.microsoft.com/en-us/cpp/preprocessor/token-pasting-operator-hash-hash Think of it as a way to stringify and concatenate arguments in a macro. In the code above, it is simply used to concatenate v, a, and r together to form var. When you compile the code, DECVAR gets replaced with macro(r, a, v), which then gets replaced with var. This gives us an int variable var, which stores 42. The value is incremented by 1 and printed.
1st May 2020, 2:35 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
Thank You Hatsy Rei ! I understood now.
1st May 2020, 3:24 PM
Wolf Master
Wolf Master - avatar