0

Can someone explain how this code works?

Hi all, in a C-challange i stumpled across this question: // begin of code #include<stdio.h> // macro definitions: #define macro(r,a,b) b##a##r #define CHAOS macro(r,a,b) // function forward declaritions: void CHAOS(int n); // main function int main(void) { bar(4); bar(2); return 0; } // function definitions: void CHAOS(int n) { printf("%d", n); } // end of code 42 is the output. I debugged the code, because i did not understand how it worked and the function CHAOS() is called at bar(). Why is that?

27th Mar 2020, 11:22 AM
m5m8s10
1 Réponse
+ 3
#define is a pre-processor directive first CHOAS is replaced by macro(r,a,b) Again we have another #define it replaces macro(r,a,b) by b##a##r ## acts as token pasting operator So b##a##r=bar Hence void CHOAS(int n) is void bar(int n) bar(4);//outputs 4 bar(2);//outputs 2 So,final output is 42
27th Mar 2020, 11:36 AM
Nandan
Nandan - avatar