[LCP]macro execution

Manu Anand badboyanand at yahoo.com
Sun Aug 5 17:41:52 UTC 2001


Hello Sri
Macros are occassional demons.Just see how your macro
will be expanded.
i=5
j=(++i*++i);
Now this could give you any result(I mean it). You are
changing value of a variable twice between 2 sequence
points and the compiler is free to do whatever it
likes.Your code is almost as bad as following code.

i=10;
printf("%d,%d,%d",i++,i++,i++);

A better way is to use:-
j=sqr(i+1);
whose answer will amaze you further.For explanation of
this answer refer to K&R2 pg 89.
(Hint::Macros are replaced token-by-token)

One more thing::Please stop voiding main.Otherwise one
fine day your program will quietly  bite you in the
leg.

Regards
Manu

--- Srinath Thiruvengadam <rangar20 at eth.net> wrote:
> please consider this macro,
>  
> *********************************
> #define sqr(x) (x*x)
> 
> void main()
> {
>   int i=5;
>   int j;
>  
>   j=sqr(++i);
>   printf("%d",j);
> }
> *********************************
> 
> it prints 49. it should execute like this      ( ++i
> * ++i )
> now, when this goes on to stack , shouldnt it go
> like this
>  
> |7 |
> |* |
> |6 |
>  
> the result should be only 42!
>  
> 
> Thanks & Regards, 
> Srinath Thiruvengadam
> 
> 


__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/



More information about the linuxCprogramming mailing list