第2个回答 2010-01-17
ISO/IEC 14882:2003(E) 中 2.3 写到:
3, If the input stream has been parsed into preprocessing tokens up to a given character, the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token, even if that would cause further lexical analysis to fail.
。。。
5, [Example: The program fragment x+++++y is parsed as x ++ ++ + y, which, if x and y are of built-in types, violates a constraint on increment operators, even though the parse x ++ + ++ y might yield a correct expression. ]
也就是说词法分析中,在一个简单的从左到右的扫描中的每个点,最长的的记号被划分,不管这样出来的记号是否有意义,所以你这个表达式会被解释成
n++ + n-- - n
但这样的表达式的结果仍是未定义的,因为在一个序列点结束之前两次改变了 n 的值,也即 n++ + n-- 这个表达式的行为是未定义的 ,所以无法根据标准计算出你这个表达式的结果,要依具体实现而定,如果在你的编译器上它的执行结果是 1 ,那你就可以暂时认为它是 1