hi,
I can't figure out how the answer came as "11" ,according to me it should be "36".
here is the code :
Code:#include <stdio.h> #define PRODUCT(x)(x*x) main( ) { int i = 5, j ; j = PRODUCT(i+1 ) ; printf ( "\n%d", j ) ; }
hi,
I can't figure out how the answer came as "11" ,according to me it should be "36".
here is the code :
Code:#include <stdio.h> #define PRODUCT(x)(x*x) main( ) { int i = 5, j ; j = PRODUCT(i+1 ) ; printf ( "\n%d", j ) ; }
Do the macro expansion, i.e.,
becomes:Code:j = PRODUCT(i+1 ) ;
which is equivalent to:Code:j = i+1 * i+1;
To avoid such problems we generally add "extra" parentheses, e.g.,Code:j = i + (1 * i) + 1;
Code:#define PRODUCT(x) ((x) * (x))
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thank you for the detailed explanation and quick reply ...i was stuck finding the error.
hey i have one more problem i am stuck at how to expand this one ?
according to me answer should be 9 ,16 as ++i and i++ are expanded as i = i+1. but ++i expands before printf and i++ after printf.
Code:#define PRODUCT(x) ( x * x ) main( ) { int i = 3, j, k ; j = PRODUCT( i++ ) ; k = PRODUCT ( ++i ) ; printf ( "\n%d %d", j, k ) ; }
Last edited by coder1; 09-10-2013 at 03:32 AM.
Refer to an earlier thread: cube program.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Although i get it how j gets 9 as answer as i is incremented after the expression is over and in this situation the expression continues till the 2nd i value get multiplied, after that only the incrementation in both i happens. sop this gives us 3*3 9 as answer.
Now in k the value of i reached is 5 and incrementation occurs before the execution of expression thus the value become 6 for i and then 7 for other ++i.
now answer should be 6*7 = 42 but it is 49 i don't get this part .
Plus I read the FAQs and what i can understand that ++i and i++ used more than once in a same expression has an undefined behavior and use of it should be avoided? did i get it right?
Last edited by coder1; 09-10-2013 at 05:28 AM.
Undefined behaviour is generally a thing to be avoided, yes.
It's not so much the macro that is causing undefined behaviour, as it is that its expansion (with parameter i++ or ++i) results in modifying a variable twice, and the sequence of those two modifications is not defined.
Yes - you must avoid using the increment opertor multiple times in the same expression. The would yield all sort of problem as you have already seen one. In all fairness, I consider it as not big problem as far as you can work out how you managed to derive such an answer. The reason for you seeing 49 is quite straight forward.
To solve the puzzle - think about it this way, a bit more logical way. The macro you have is nothing more than times the given number. Therefore, if you have 49 the only way to get 49 is by this 7 * 7. Now try workout, is there a way from you code that your can be incremented to 7.
I'm sure others could may have explained it much better. But it is harder to put it in words.Code:i = 3; j = PRODUCT( i++ ); ===> i = 3 k = PRODUCT( ++i ); ===> PRODUCT( ++i ) ==> ( ++i * ++i ) /* In total you have ++i about three times. Which is basically 4 + 3 = 7. Which is what the final expression get evaluted on. But as you can how and in what sequence the complier call those incremental is undefined making it harder to evaluate manually. Hence therefor must avoid using such expression */
ssharish
Life is like riding a bicycle. To keep your balance you must keep moving - Einstein
They are called macro function and arguments. Read this
Macro Arguments - The C Preprocessor
ssharish
Life is like riding a bicycle. To keep your balance you must keep moving - Einstein