Amazon Interview Question
When using the preprocessor operators (#, ##, #@ (only for Microsoft)) in the macro, the arguments are treated as is, that is without trying to search any other macros within it.
same2(concatinate(1,2)) --->same1(concatinate(1,2)) ---->same1((1##2)) ->#(12)->"12";
same1(concatinate(1,2))--->#(concatinate(1,2))->"concatinate(1,2)"
Note after “#”“##” is scanned , no macro can be treated as macro, they will be treated as string.
It looks to me like Macro is from left to right BFS instread of DFS
I wonder why amazon even bothers with this question? If the candidate gets it wrong, what does that mean? If he/she gets it right, what does that mean?
Really amazon, get a clue here.. :)
I had the exact same feelings as above person when I saw this question.
It doesn't tell much at all about the interviewee. Even if s/he gets it wrong:
1) He may just got confused
2) He may just didn't know ## crap
3) He hates macros anyways, so => Make him the director of a S/w org at Amazon LOL..
When the compiler comes to this line:
printf("%s\n",same2(concatinate(1,2)));
it sees same2 for which it replaces it with same1.
Now, it sees concatinate(1, 2). It immediately replaces it with 12.
When it comes to same1, it sees a definition and replaces the rest of the code with #a i.e. concatinate(1, 2).
So, the compiler actually does the same2 in pass two. And same1 in pass one.
12
- fiddler.g July 16, 2010concatinate(1,2)