LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 799|回复: 4

+= or + = ?

[复制链接]
发表于 2005-5-23 09:19:36 | 显示全部楼层 |阅读模式
C Traps and Pitfalls中说

  1. Compound assignment operators such as += are [color=Red]really multiple tokens[/color]. Thus,
  2. a + /* strange */ = 1
  3. means the same as
  4. a += 1
  5. These operators are the [color=Red]only cases[/color] in which things that look like single tokens are really multiple tokens.
复制代码

但我在gcc4下发现写成+ =后编译不能成功!
不知道是不是gcc不支持?
还是。。。。。。。 :help  :help
发表于 2005-5-23 10:02:27 | 显示全部楼层
"+="                        { count(); return(ADD_ASSIGN); }
"-="                        { count(); return(SUB_ASSIGN); }
"*="                        { count(); return(MUL_ASSIGN); }
"/="                        { count(); return(DIV_ASSIGN); }
"%="                        { count(); return(MOD_ASSIGN); }
"&="                        { count(); return(AND_ASSIGN); }
"^="                        { count(); return(XOR_ASSIGN); }
"|="                        { count(); return(OR_ASSIGN); }

看看ANSI C词法就知道了,"+=“ 是一个token
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-23 12:31:08 | 显示全部楼层
Post by welans
"+="                        { count(); return(ADD_ASSIGN); }
"-="                        { count(); return(SUB_ASSIGN); }
"*="                        { count(); return(MUL_ASSIGN); }
"/="                        { count(); return(DIV_ASSIGN); }
"%="                        { count(); return(MOD_ASSIGN); }
"&="                        { count(); return(AND_ASSIGN); }
"^="                        { count(); return(XOR_ASSIGN); }
"|="                        { count(); return(OR_ASSIGN); }

看看ANSI C词法就知道了,"+=“ 是一个token

你的意思是说C Traps and Pitfalls里讲错了?
回复 支持 反对

使用道具 举报

发表于 2005-5-23 13:04:11 | 显示全部楼层
在ANSI C Standard (1999) 第6.4.6 punctuators 一节提到的:

  1. punctuator: one of
  2.        [ ] ( ) { } . ->
  3.        ++ -- & * + - ˜ !
  4.        / % << >> < > <= >=  ==  !=  &#710;   |  && ||
  5.        ? : ; ...
  6.        = *= /= %= += -= <<= >>=  &=  &#710;=   |=
  7.        , # ##
  8.        <: :> <% %> %: %:%:
复制代码

该标准目前应该没有变化吧。
回复 支持 反对

使用道具 举报

发表于 2005-5-23 14:47:19 | 显示全部楼层
C Traps and Pitfalls写作年代比较早。可能那个时候的编译器还能支持这种特性。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表