我似乎无法在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 …
我正在处理一些包含表单表达式的代码
-(sizeof(struct foo))
Run Code Online (Sandbox Code Playgroud)
即a的否定,size_t我不清楚编译器在看到这个时C和C++标准需要什么.具体来说,从此处和其他地方四处查看,sizeof返回类型的无符号整数值size_t.当否定无符号整数时,我找不到任何指定行为的明确引用.有没有,如果有的话,它是什么?
编辑:好的,所以关于无符号类型的算术有一些很好的答案,但事实并非如此.如果否定,它是在无符号整数上运行,还是转换为有符号类型并对其执行某些操作?从标准中预期的行为是"想象它是相似幅度的负数,然后对无符号值应用'溢出'规则"?