能力值:
( LV2,RANK:10 )
|
-
-
[求助]C语言的自增
完全同意,这跟C语言中定义的序列化点有关。
C语言在两个序列化点之间,只允许写同一变量一次,并且如果既读又写,那么一定是只能用读出来的值来计算要写进去的值。(很含糊吧)
所谓序列化点包括
a;b;(分号)保证 a在b前执行
if(a){b} while(a){b} 保证 a在b前执行
(a,b) (逗号运算符,与函数调用的分隔符不一样) 保证a在b前执行
a=a+b; 可以,符合读出来的值用来计算写入的值
a && b 保证a在b前执行
a || b 也是一样
等等
特别注意的是自增、赋值、加减乘除、位与或等等都不是序列点,
这个不仅仅是优先级的问题,加括号也不能改变(其实括号就只是用来强制优先级,但是优先级规定的结合性,而不是哪个先计算,这与我们平时的自然语言习惯有不同)
这里的“在……之前”只是从效果上来说,编译器优化是允许的(意即除了代码变快不能有别的可观察的区别)
至于未定义行为,标准要求编译器自行选择合理(但没有规定什么叫合理 )的行为。
违反了规定,编译器可以给警告(要打开关,比如gcc的-Wall -W就会警告这种错误),也可以不给(因为很难检测)
我记得Java就规定了一大堆的东西,比如a+b中a要在b前计算。(我不熟悉Java)
|