关于a++和++a

1.写出判断ABCD四个表达式的是否正确, 若正确, 写出经过表达式中 a的值(3分)
int a = 4;
(A)a += (a++); (B) a += (++a) ;(C) (a++) += a;(D) (++a) += (a++);
a = ?
答:C错误,左侧不是一个有效变量,不能赋值,可改为(++a) += a;

 

 

no   need   to   be   sorry,   you   made   a   good   point.   :-)  
  constant   objects   do   have   names,   but   can't   be   used   as   lvalues.   that   is   pretty   clear.  
   
  the   reason   for   that   i   said   '++a'   had   a   name   is   because   '++a'   is   equivalent   to   this:   'a   =   a   +   1;   a;'   i.e.   the   final   result   is   'a',   which   has   incremented   by   1;   on   the   contrary,   'a++'   means   this:   'tmp   =   a;   a   =   a   +   1;   tmp;',   i.e.   the   final   result   is   a   temporary   object,   which   is   equal   to   the   'a'   before   incrementation.   the   temporary   doesn't   have   a   name,   so   it   can't   be   a   lvalue.  
   
  if   your   question   is   about   how   to   judge   whether   a   value   can   be   used   as   a   lvalue,   that   is   a   good   rule   of   thumb   (not   necessarily   be   the   lvalue's   definition   though),   i.e.   all   named   objects   (excluding   constants,   and   objects   of   some   special   user-defined   classes   (they   can   be   made   un-assignable)   surely)   can   be   used   as   lvalues,   unnamed   objects   can   not.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值