Board index » Visual Studio » About the problem "b++"

About the problem "b++"

Visual Studio384
Hi,everyone



When I first learned VC++ 6.0,I met a problem about "b++".The code as

followed,



#include <iostream.h>

void main()

{

int b(5),c(3);

a=b+++--c;

cout<<a<<','<<b<<','<<c<<endl;

}



As the rules, "a=b+++--c" should be calculated as "a=b++ + --c".When I run

the program,the result is 7,6,2.But I don't understand why it display

"7",why not "8"?



Thanks in advance,

Ely


-
 

Re:About the problem "b++"

Elynis Zhou wrote:

Quote
Hi,everyone



When I first learned VC++ 6.0,I met a problem about "b++".The code as

followed,



#include <iostream.h>

void main()

{

int b(5),c(3);

a=b+++--c;

cout<<a<<','<<b<<','<<c<<endl;

}



As the rules, "a=b+++--c" should be calculated as "a=b++ + --c".When I

run the program,the result is 7,6,2.But I don't

understand why it display "7",why not "8"?



b = 5

c = 3

temp = b

b = b+1

c = c-1

a = temp + c



That's the difference between b++ "post increment" and ++b "pre increment" -

the value of b++ is the value of b before the increment, but b is

incremented nonetheless. On the other hand, the value of ++b is the value

after the increment. The case for pre- and post-decrement is analogous.



-cd





-

Re:About the problem "b++"

CD,thank you very much for you explaination and help.

I checked --/++* and *--/++ .It's ok now.

o(¡É_¡É)o...



"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam>

дÈëÏûÏ¢ÐÂÎÅ:%23MfyypOVIHA.5508@TK2MSFTNGP04.phx.gbl...

Quote
Elynis Zhou wrote:

>Hi,everyone

>

>When I first learned VC++ 6.0,I met a problem about "b++".The code as

>followed,

>

>#include <iostream.h>

>void main()

>{

>int b(5),c(3);

>a=b+++--c;

>cout<<a<<','<<b<<','<<c<<endl;

>}

>

>As the rules, "a=b+++--c" should be calculated as "a=b++ + --c".When I

>run the program,the result is 7,6,2.But I don't

>understand why it display "7",why not "8"?



b = 5

c = 3

temp = b

b = b+1

c = c-1

a = temp + c



That's the difference between b++ "post increment" and ++b "pre

increment" - the value of b++ is the value of b before the increment, but

b is incremented nonetheless. On the other hand, the value of ++b is the

value after the increment. The case for pre- and post-decrement is

analogous.



-cd









-

Re:About the problem "b++"

On Fri, 11 Jan 2008 23:12:08 -0800, "Carl Daniel [VC++ MVP]"

<cpdaniel_remove_this_and_nospam@mvps.org.nospam>wrote:



Quote
Elynis Zhou wrote:

>Hi,everyone

>

>When I first learned VC++ 6.0,I met a problem about "b++".The code as

>followed,

>

>#include <iostream.h>

>void main()

>{

>int b(5),c(3);

>a=b+++--c;

>cout<<a<<','<<b<<','<<c<<endl;

>}

>

>As the rules, "a=b+++--c" should be calculated as "a=b++ + --c".When I

>run the program,the result is 7,6,2.But I don't

>understand why it display "7",why not "8"?



b = 5

c = 3

temp = b

b = b+1

c = c-1

a = temp + c



That's the difference between b++ "post increment" and ++b "pre increment" -

the value of b++ is the value of b before the increment, but b is

incremented nonetheless. On the other hand, the value of ++b is the value

after the increment. The case for pre- and post-decrement is analogous.



-cd





another tip is to avoid such code, other (maintenance) programmers

will otherwise be pissed off at you.



try to keep code simple and clear and let the compiler worry about

optimization.



br/ajk

-

Re:About the problem "b++"



"ajk" <ak@workmail.com>wrote in message

Quote
On Fri, 11 Jan 2008 23:12:08 -0800, "Carl Daniel [VC++ MVP]"

<cpdaniel_remove_this_and_nospam@mvps.org.nospam>wrote:



>Elynis Zhou wrote:

>>Hi,everyone

>>

>>When I first learned VC++ 6.0,I met a problem about "b++".The code as

>>followed,

>>

>>#include <iostream.h>

>>void main()

>>{

>>int b(5),c(3);

>>a=b+++--c;

>>cout<<a<<','<<b<<','<<c<<endl;

>>}

>>

>>As the rules, "a=b+++--c" should be calculated as "a=b++ + --c".When I

>>run the program,the result is 7,6,2.But I don't

>>understand why it display "7",why not "8"?

>

>b = 5

>c = 3

>temp = b

>b = b+1

>c = c-1

>a = temp + c

>

>That's the difference between b++ "post increment" and ++b "pre

>increment" -

>the value of b++ is the value of b before the increment, but b is

>incremented nonetheless. On the other hand, the value of ++b is the value

>after the increment. The case for pre- and post-decrement is analogous.

>

>-cd

>



another tip is to avoid such code, other (maintenance) programmers

will otherwise be pissed off at you.



try to keep code simple and clear and let the compiler worry about

optimization.



Well, the problem with that code is letting operators flow together. IMHO

this would be much better and any C programmer should be able to read:



a = (b++) - (--c);



Quote


br/ajk





-