HI,

I am not sure whether i am right or wrong pls correct me if i am 
wrong.
 
--- In [EMAIL PROTECTED], Sreeram B S 
<[EMAIL PROTECTED]> wrote:
> Hi,
>   As Shyan has rightly mentioned, such kind of assignments and 
> increments in a single statement result in **undefined behaviour**.
>   But the result what you have got when you have executed the 
> program is well-explainatory. 
>  
> Your program:
> 
> #include<stdio.h>
> void main()
> {
>         int i=5;
>         i = (++i) /( i++); // stm 1
>         printf("%d",i); // stm 2
> }
> output = 2
>    ++i=6 ; since i=5 and pre-increment occurs first.  Now i=6/5 
> which is 1. The denominator is 5, because the post-increment 
> happens, after the complete execution of that sentence. So, at 

When value of i has incremented to 6 in numerator then how can 
denominator be 5 (same variable i is used in denominator)

> this point i=1. Now the statement is executed and post-increment 
> of i (i++) is remaining. So, i gets incremented and results in 2.

what about the parenthesis in the denominator. (I was taught that 
parenthesis has the highest priority and is evaluated first before 
any arithmetic operator) so i think that denominator will be 
evaluated before division. see the stmt it says
i = (++i) / (i++);
so the numerator & denominator should be evaluated before division.

> 
> If you put the same in: printf("%d",(++i)/(i++)); --  This is as 
> good as executing only 6/5 which is 1 and incrementing of i after 
> the printf execution is completed. So, the answer what you have 
> obtained is correct ie 1. But the final value of i is now 2. So, 
> if you add another printf statement as : printf("%d",i); -- you 
> will get 2.
> 

i think as Shyan had said that this is **undefined behaviour** (also 
quoted above) so there should be no explanation as to why this 
happens & it totaly depends on compiler.


Bye
Sumit








------------------------ Yahoo! Groups Sponsor --------------------~--> 
Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
Now with Pop-Up Blocker. Get it for free!
http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/EbFolB/TM
--------------------------------------------------------------------~-> 

To unsubscribe : [EMAIL PROTECTED]

 
Yahoo! Groups Links

<*> To reply to this message, go to:
    
http://groups.yahoo.com/group/Programmers-Town/post?act=reply&messageNum=4568
    Please do not reply to this message via email. More information here:
    http://help.yahoo.com/help/us/groups/messages/messages-23.html

<*> To visit your group on the web, go to:  
    http://groups.yahoo.com/group/Programmers-Town/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to