免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1455 | 回复: 5
打印 上一主题 下一主题

[C] ++i 在C 为什么不是左值 [复制链接]

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:49:03
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-14 08:10 |只看该作者 |倒序浏览
++i 在C 为什么不是左值 , 但是在C++ 是左值


  1. #include <stdio.h>

  2. int main(void)
  3. {
  4.           int c = 0;
  5.           ++c = 100;
  6.           printf("c = %d\n",c);
  7.           return 0;
  8. }
复制代码


gcc 无法编译成功 , g++ 可以

谢谢

[ 本帖最后由 shihyu 于 2009-3-14 09:00 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-03-14 08:41 |只看该作者
有吗?

论坛徽章:
0
3 [报告]
发表于 2009-03-14 09:37 |只看该作者
常量可以是左值么???

论坛徽章:
0
4 [报告]
发表于 2009-03-14 09:39 |只看该作者
规定就是规定,或许以后会变成你期望的方式

论坛徽章:
0
5 [报告]
发表于 2009-03-14 10:36 |只看该作者
原帖由 shihyu 于 2009-3-14 08:10 发表
++i 在C 为什么不是左值 , 但是在C++ 是左值


#include

int main(void)
{
          int c = 0;
          ++c = 100;
          printf("c = %d\n",c);
          return 0;
}

gcc 无法编译 ...


C标准规定的. 在 6.3.2节中
1 Anlvalue is an expression with an object type or an incomplete type other than void;53)
if an lvalue does not designate an object when it is evaluated, the behavior is undefined.
When an object is said to have a particular type, the type is specified by the lvalue used to
designate the object. A modifiable lvalue is an lvalue that does not have array type, does
not have an incomplete type, does not have a const-qualified type, and if it is a structure
or union, does not have any member (including, recursively, any member or element of
all contained aggregates or unions) with a const-qualified type.

2 Except when it is the operand of the sizeof operator, the unary & operator, the ++
operator, the -- operator, or the left operand of the . operator or an assignment operator,
an lvalue that does not have array type is converted to the value stored in the designated
object (and is no longer an lvalue). If the lvalue has qualified type, the value has the
unqualified version of the type of the lvalue; otherwise, the value has the type of the
lvalue. If the lvalue has an incomplete type and does not have array type, the behavior is
undefined.

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
6 [报告]
发表于 2009-03-14 13:30 |只看该作者
原帖由 shihyu 于 2009-3-14 08:10 发表
++i 在C 为什么不是左值 , 但是在C++ 是左值

C 和 C++ 是两种不同的语言,你怎么能指望它们的语义相同?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP