我似乎无法在 C 标准中找到完全定义具有无符号操作数的一元减运算符的行为的相关部分。
2003 C++ 标准(是的,C++,请耐心等待几行)在 5.3.1c7 中说:The negative of an unsigned quantity is computed by subtracting its value from 2^n, where n is the number of bits in the promoted operand.
然而,1999 年的 C 标准不包括这样一个明确的声明,也没有明确定义一元 - 行为,无论是在 6.5.3.3c1,3 还是在 6.5c4 中。在后者中它说Some operators (the unary operator ~, and the binary operators <<, >>, &, ^, and |, ...) ... return values that depend on the internal representations of integers, and have implementation-defined and undefined aspects for signed types.)
,它排除了一元减号并且事情似乎仍然模糊。
这个较早的问题是指 K&R ANSI C 书的 A.7.4.5 节,其中说The negative of an unsigned quantity is computed by subtracting the promoted value from the largest value of the promoted type and adding one
.
什么是 1999 年的 C 标准,相当于书中的上述引用?
6.2.5c9 说:A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.
是这样吗?还是我还缺少其他东西?