This is a discussion on how is the precedence and associativity? within the C and C++ forums, part of the Programming Talk category; Hi, in an interview question i was asked following question #define multiply(x) x*x main() { int result; result = multiply(...
|
|||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
how is the precedence and associativity?
Hi,
in an interview question i was asked following question #define multiply(x) x*x main() { int result; result = multiply(2) + 1; } what is the result value? i told the answer is 5 since the macro gets expanded as 2*2+1 and here the multiplication takes first and addition latter. but the interviewer said ans is 6 since the evaluation takes place from Left to right so first addition and then multiplcation. i am not sure now what is correct ? i think * and + have L to R associativity and * has higher precedence. Can someone correct me if i am wrong? thanks a lot in advance |