首页 新闻 会员 周边

求助,谢谢帮助

0
悬赏园豆:10 [已解决问题] 解决于 2011-10-11 15:35

int main()
{
 int a=0;
 int b=1;
 ++a=2;       (*)

 printf("%d,%d\n",a,b);

 return 0;
}

(*)式是不对的,显然是把一个值赋给了另一个值,在C和指针这本书里也说这种表达式是不对的,
但在VC6.0,和CFree上都可以运行通过,这是为什么呢?

lzjnhs的主页 lzjnhs | 初学一级 | 园豆:60
提问于:2011-05-04 09:42
< >
分享
最佳答案
0

The return value of (++a) is a l-value instead of r-value, according to MSDN explained:

http://msdn.microsoft.com/en-us/library/dy3d35h8(v=vs.80).aspx

 

But, I think the standard c specification do not restrict the type of return value.(l-value or r-right are both accepted by specification).

 

You can read the c specification at http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf

In section of [6.5.3 Unary operators] the c standard just requests the value of ++a equal to (a+=1). and do not metion the type of return value.

I think the things underneath is inserting a [Seqence Point] after the prefix increment taking effect.

 

 

 

If you test your code in gcc, you may see the compiling error occurred when compiling the code. as:

error: lvalue required as left operand of assignment


收获园豆:10
Jerry Chou | 老鸟四级 |园豆:2642 | 2011-05-04 11:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册