++ problem in C

C已经忘得差不多了,再学习,遇到个++问题。记录一下。

following codes are wrote in C by using code::blocks + WinGW.
[code]
int a = 0;
a = (a++) + (a++);
printf("%d", a);
[/code]

in my opinion (most of which from ActionScript coding), first “a++” make a’s value increase to “1″ and return 0, next “a++” make a’s value increase to “2″ and return 1, so the resulte is 0 + 1 and assign back to a, so print 1. but it shows 2.

i modified code to:

[code]
int a = 0;
int b;
b = (a++) + (a++);
printf("%d", b);
[/code]

it is strange, value “0″ is shown on screen. much different from Java and AS as i known before.

i am not sure, but is have these results:

1, the strange print value is caused by compiler.
2, in WinGW, if you use ++(–) to the same value, it returns the original value.
3, in WinGW, one variate cannot be used in both L-exp and R-exp.

i think i find the answer, see this, and this.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>