本帖最后由 pmerofc 于 2010-04-06 22:17 编辑
应某出版社之约,拟就国内C语言“主流教材”中的错误、误导和不良风格等写点东西。
在正式动笔之前和大家(除了“星宿”派的弱智以外)交流一下,非常希望能得到大家的批评、指正和建议。
QUOTE:赋值表达式也可以包括复合的赋值运算符。例如:
a+=a-=a*a
也是一个赋值表达式。如果a的初值为12,此赋值表达式的求解步骤如下:
①先进行“a-=a*a”的运算,它相当于a=a-a*a,a的值为12-144=132。
②再进行“a+=-132”的运算,相当于a=a+(-132),a的值为-132-132=-264。
p64,《C程序设计》(第三版),谭浩强 著,清华大学出版社,2005年7月第3版,2009年10月第26次印刷
评:“a+=a-=a*a”这个表达式本身就是错误的,怎么居然还能分析出求解步骤呢?
]
1 我认为回避“ a+=a-=a*a 对不对”是自欺欺人
2 这不是细节,是常识
3 程序恰恰是细节组成的,不懂得这一点根本不懂编程
Annex C
(informative)
Sequence points
1The following are the sequence points described in 5.1.2.3:
—The call to a function, after the arguments have been evaluated (6.5.2.2).
—The end of the first operand of the following operators: logical AND && (6.5.13);
logical OR || (6.5.14); conditional ? (6.5.15); comma , (6.5.17).
—The end of a full declarator: declarators (6.7.5);
—The end of a full expression: an initializer (6.7.; the expression in an expression
statement (6.8.3); the controlling expression of a selection statement (if or switch)
(6.8.4); the controlling expression of a while or do statement (6.8.5); each of the
expressions of a for statement (6.8.5.3); the expression in a return statement
(6.8.6.4).
—Immediately before a library function returns (7.1.4).
—After the actions associated with each formatted input/output function conversion
specifier (7.19.6, 7.24.2).
—Immediately before and immediately after each call to a comparison function, and
also between anycall to a comparison function and anymovement of the objects
passed as arguments to that call (7.20.5).
应某出版社之约,拟就国内C语言“主流教材”中的错误、误导和不良风格等写点东西。
在正式动笔之前和大 ...
p64,《C程序设计》(第三版),谭浩强 著,清华大学出版社,2005年7月第3版,2009年10月第26次印刷
评:“a+=a-=a*a”这个表达式本身就是错误的,怎么居然还能分析出求解步骤呢?
因为C的编译器一向只对违背Constraints的代码报错,而对那些“似是而非”的错误一般只给出警告,这是C一贯的风格
比如
int i;
scanf ("%d",i);
我相信C的编译器也只是给出警告而已,不会报错。尽管我们都知道那的确错了
我没用过gcc。如果gcc给出警告,说明gcc是进化得非常不错的编译器,而且和前面所说的“a+=a-=a*a”是错误代码的观点相互吻合
欢迎光临 Chinaunix (http://bbs.chinaunix.net/) | Powered by Discuz! X3.2 |