一元算术运算符Unary Arithmetic Operators

下面的列表中讨论了 C 一元加、算术求反、求补和逻辑求反运算符:The C unary plus, arithmetic-negation, complement, and logical-negation operators are discussed in the following list:

运算符Operator 说明Description
+ 括号内的表达式前面的一元加运算符强制对包含的运算进行分组。The unary plus operator preceding an expression in parentheses forces the grouping of the enclosed operations. 它用于涉及多个结合的或可交换的二元运算符的表达式。It is used with expressions involving more than one associative or commutative binary operator. 操作数必须具有算法类型。The operand must have arithmetic type. 结果为操作数的值。The result is the value of the operand. 整型操作数将进行整型提升。An integral operand undergoes integral promotion. 结果的类型为提升后的操作数的类型。The type of the result is the type of the promoted operand.
- 算术求反运算符生成其操作数的负值(2 的补数)。The arithmetic-negation operator produces the negative (two's complement) of its operand. 操作数必须是整型值或浮点值。The operand must be an integral or floating value. 此运算符执行常用算术转换。This operator performs the usual arithmetic conversions.
~ 按位求补(或按位“非”)运算符将产生其操作数的按位补数。The bitwise-complement (or bitwise-NOT) operator produces the bitwise complement of its operand. 操作数必须为整型。The operand must be of integral type. 此运算符执行常用算术转换;结果具有转换后的操作数的类型。This operator performs usual arithmetic conversions; the result has the type of the operand after conversion.
!! 如果其操作数为 true(非零),则逻辑求反(逻辑“非”)运算符将生成值 0;如果其操作数为 false (0),则生成值 1。The logical-negation (logical-NOT) operator produces the value 0 if its operand is true (nonzero) and the value 1 if its operand is false (0). 结果具有 int 类型。The result has int type. 操作数必须是整型值、浮点值或指针值。The operand must be an integral, floating, or pointer value.

指针上的一元算术运算是非法的。Unary arithmetic operations on pointers are illegal.

示例Examples

下面的示例演示了一元算术运算符:The following examples illustrate the unary arithmetic operators:

short x = 987;  
    x = -x;  

在上面的示例中,x 的新值为 987 的负数或 -987。In the example above, the new value of x is the negative of 987, or -987.

unsigned short y = 0xAAAA;  
    y = ~y;  

在此示例中,赋给 y 的新值是无符号值 0xAAAA 或 0x5555 的二进制反码。In this example, the new value assigned to y is the one's complement of the unsigned value 0xAAAA, or 0x5555.

if( !(x < y) )  

如果 x 大于或等于 y,则该表达式的结果为 1 (true)。If x is greater than or equal to y, the result of the expression is 1 (true). 如果 x 小于 y,则结果为 0 (false)。If x is less than y, the result is 0 (false).

另请参阅See Also

使用一元运算符的表达式Expressions with Unary Operators