Well if you are taking argument of left and right to
say that my reasons are wrong.
well again Mr.
look
at the statement again
v=(m++)+(++m);
i just executed it from left to right and now i am executing it from right to
left.
See!
first we have ++m
it means that m must be increase by 1 before the statement is executed.Ok if m
is initialized with 5 then it will be set to 6
so the value of m is now 6 before we reach the next operand.
now the next operand is m++
which means nothing but take value of m
i.e. 6 and after the execution of full expression set it to 7(increment by1)
now the result will be generated from right to left
will be as m(increment by 1) + (value of m)=
5(increment by 1)+(value of mi.e now 6)=12;
and so the answer.
And i think it is straight forward answer directly from the basic knoledge.
----- Forwarded Message ----
From: Arvind Balodia <[email protected]>
To: [email protected]
Sent: Wednesday, May 27, 2009 8:39:51 PM
Subject: Re: [c-prog] confusing output
Well you said that it depends on compilers whether it works left to right or
right to left but in both the cases the answer will be same provided that the
two operands must be different .But in case of same operands as here m , you
are telling that it is against the rule of c programming, but tell why the
compiler is giving me the results 12 and 12 to me for both the expression and
11 and 12 as in the original problem.There must be a strategy that each
compiler must follow. I think you are right but you have not given the complete
answer.
lets have a following code.
v=(m++)+(++m);
if it is against the rule of c language then the all the compilers that have
been developed should leave a message telling the User that it is against the
programming laws.
But i have not read a single law like this in any book,
And perhaps the groups of people who have developed
the compilers of me and of the
person from whom the problem comes.
I think your answer does not make any sense
among computer students!
________________________________
From: Tamas Marki <[email protected]>
To: [email protected]
Sent: Wednesday, May 27, 2009 8:27:12 PM
Subject: Re: [c-prog] confusing output
On Wed, May 27, 2009 at 4:19 PM, Arvind Balodia
<arvind.balodia@ yahoo.com> wrote:
> Ok well i checked for your output but unlike you
> i found the same answer in both the cases and that was that
> it must be i.e. 12;
> In the assigning statement of v
> you are telling the compiler like this
> (m++)+(++m)
> it means first take m and after the execution increase its value by 1 and
> add m again but before the execution increase its value by 1
> so before the execution of the above statement the value of m
> is set to 6(by an increment of 1)
> and yeah the result will be generated as m+m
> i.e.6+6 =12
> after the execution of statement the m
> will be set to 7
> So the answer is 12.
> please have a look once again on your program.
Please don't encourage the use of such constructs. As Paul already
pointed out, you are not allowed to modify one variable more than once
between two sequence points.
What you are experiencing is undefined behavior. A compiler could
generate code that overwrites the hard drive with garbage data for
such a code, and still be standards-compliant .
Repeat after me: I will not change a variable more than once between
two sequence points!
--
Tamas Marki
[Non-text portions of this message have been removed]