On Wed, May 27, 2009 at 10:07 AM, Aliya Awais <[email protected]> wrote:
> hi ! can someone please tell me the difference between output of given two
> expression in C++ programs , how expressions are evaluated in these two
> cases?
>
> #include<iostream.h>
<iostream.h> is a deprecated header (if it was ever Standard in the
first place - I can't remember.)
You should use <iostream> (without the .h) instead.
> void main()
main returns int, not void, float or struct foo.
> {
> int m=5,v=0,u=0;
> v=v+(m++)+(++m);
You're modifying m twice between sequence points ( or 'in a single
statement' in simpler, but less accurate terms.)
This is not allowed in C or C++: http://c-faq.com/expr/evalorder2.html
While your compiler may accept it, the end result of the calculation
is not defined, and any answer the compiler comes up with is
technically correct. It may differ between platforms, compilers, or
even invocations of the same binary. All answer are 'correct.'
> cout<<v<<endl; //output is 11
> }
>
> #include<iostream.h>
> void main()
> {
> int m=5,v=0,u=0;
> v=(m++)+(++m);
> cout<<v<<endl; //output is 12 (why confusing)
> }
>
> what is difference between v=v+(m++)+(++m); and v=(m++)+(++m); .
There is no 'difference' in that they're both wrong for the same
reason - I've indicated above why they're returning different answers.
Both answers are correct - it is the program that's wrong.
--
PJH
http://shabbleland.myminicity.com/com
http://www.chavgangs.com/register.php?referer=9375