Cprogramming 简明教程

Unary Operators in C

虽然 C 中的大多数运算符本质上都是二元(binary)的,但也有一些一元运算符。如果运算符只需要一个操作数,则称之为一元,这不同于需要两个操作数的二元运算符。

While most of the operators in C are binary in nature, there are a few unary operators as well. An operator is said to be unary if it takes just a single operand, unlike a binary operator which needs two operands.

C 中的一些运算符在使用上既是二元也是一元。C 中一元算子的示例包括 ++--! 等。

Some operators in C are binary as well as unary in their usage. Examples of unary operators in C include ++, --, !, etc.

The Increment Operator in C

自增运算符(++)向其操作数变量的值加 1,并将其重新赋值给该变量。

The increment operator (++) adds 1 to the value of its operand variable and assigns it back to the variable.

a 语句等价于写成 “ a = a + 1 ”。“"" 运算符可以在操作数之前或之后出现,效果相同。因此, a 等价于 a

The statement a is equivalent to writing "a = a + 1." The "" operator can appear before or after the operand and it will have the same effect. Hence, a is equivalent to a.

然而,当自增运算符与表达式中的其他运算符一起出现时,其效果并不相同。“前缀” 的优先级高于 “后缀”。因此,“ b = a; ” 与 “ b = a; ” 不同。

However, when the increment operator appears along with other operators in an expression, its effect is not the same. The precedence of "prefix " is more than "postfix ". Hence, "b = a;" is not the same as "b = a;"

在第一种情况下,“a” 在自增之前赋值给 “b”;而在第二种情况下,自增在赋值之前执行。

In the former case, "a" is assigned to "b" before the incrementation; while in the latter case, the incrementation is performed before the assignment.

The Decrement Operator in C

递减运算符 (--) 会从操作数变量的值中减去 1,然后将其重新赋值给该变量。

The decrement operator (--) subtracts 1 from the value of its operand variable and assigns it back to the variable.

a--; 语句等价于写成 “ a = a - 1;

The statement "a--;" is equivalent to writing "a = a - 1;"

“--” 运算符可以在操作数之前或之后出现,在任何情况下,效果都是相同的。因此,“ a-- ” 等价于 “ --a ”。

The "--" operator can appear before or after the operand and in either case, it will have the same effect. Hence, "a--" is equivalent to "--a".

然而,当递减运算符与表达式中的其他运算符一起出现时,其效果并不相同。“前缀 --” 的优先级高于 “后缀 --”。因此,“ b = a-- ” 与 “ b = --a ” 不同。

However, when the decrement operator appears along with other operators in an expression, its effect is not the same. The precedence of "prefix --" is more than "postfix --". Hence, "b = a--" is not the same as "b = --a".

在第一种情况下,“a” 在自减之前赋值给 “b”;而在第二种情况下,自减在赋值之前执行。

In the former case, "a" is assigned to "b" before the decrementation; while in the latter case, the decrementation is performed before the assignment.

The Unary "+" Operator in C

“+” 和 “–” 运算符是众所周知的二进制加法和减法运算符。然而,它们也可以用作一元运算。当用作一元运算时,它们被前缀添加到操作数变量。

The "+" and "–" operators are well known as binary addition and subtraction operators. However, they can also be used in unary fashion. When used as unary, they are prefixed to the operand variable.

每当正值赋值给任何数字变量时,“+” 运算符会隐式存在。 int x = 5; 语句与 int x = +5; 相同。相同的逻辑也适用于浮点和字符变量。

The "+" operator is present implicitly whenever a positive value is assigned to any numeric variable. The statement "int x = 5;" is same as "int x = +5;". The same logic applies to float and char variable too.

Example

请看以下示例:

Take a look at the following example −

#include <stdio.h>

int main(){

   char x = 'A';
   char y = +x;

   float a = 1.55;
   float b = +a;

   printf ("x: %c y: %c\n", x,y);
   printf ("a: %f y: %f\n", a,b);

   return 0;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

x: A y: A
a: 1.550000 y: 1.550000

The Unary "−" Operator in C

通常表示减法运算符的 “ ” 符号在 C 中还充当一元否定运算符。以下代码展示了如何在 C 中使用一元否定运算符。

The "" symbol, that normally represents the subtraction operator, also acts the unary negation operator in C. The following code shows how you can use the unary negation operator in C.

Example

在此代码中,一元否定运算符返回 “x” 的负值,并将相同的值赋值给另一个变量 “y”。

In this code, the unary negation operator returns the negative value of "x" and assigns the same to another variable "y".

#include <stdio.h>

int main(){

   int x = 5;
   int y = -x;

   printf("x: %d y: %d\n", x, y);

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

x: 5 y: -5

The Address-of Operator (&) in C

我们在 C 中使用 & 符号作为二进制 AND 运算符。然而,我们也使用相同的 & 符号作为一元运算,即 “取地址” 运算符。

We use the & symbol in C as the binary AND operator. However, we also use the same & symbol in unary manner as the "address-of" operator.

Example

& 运算符返回其变量操作数的内存地址。请看以下示例:

The & operator returns the memory address of its variable operand. Take a look at the following example −

#include <stdio.h>

int main(){

   char x = 'A';
   printf ("Address of x: %d\n", &x);

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

Address of x: 6422047

Note :每次声明变量时,C 编译器都会分配一个随机内存地址。因此,每次打印地址时,结果都可能不同。

Note: The C compiler assigns a random memory address whenever a variable is declared. Hence, the result may vary every time the address is printed.

格式说明符 %p 用于获取内存地址的十六进制表示形式。

The format specifier %p is used to get a hexadecimal representation of the memory address.

char x = 'A';
printf ("Address of x: %p\n", &x);

这将以 16 进制格式打印“x”的内存地址:

This prints the address of "x" in hexadecimal format −

Address of x: 000000000061FE1F

变量的地址通常存储在 “指针变量” 中。指针变量用一个 “*” 前缀声明。在下面的代码片段中,“x” 是一个普通整数变量,而 “y” 是一个指针变量。

The address of a variable is usually stored in a "pointer variable". The pointer variable is declared with a "*" prefix. In the code snippet below, "x" is a normal integer variable while "y" is a pointer variable.

int x = 10;
int *y = &x;

The Dereference Operator (*) in C

我们通常使用 “*” 符号作为乘法运算符。然而,它在 C 中也用作 “解引用运算符”。

We normally use the "*" symbol as the multiplication operator. However, it is also used as the "dereference operator" in C.

当你想存储变量的内存地址时,该变量应该用一个星号 (*) 前缀声明。

When you want to store the memory address of a variable, the variable should be declared with an asterisk (*) prefixed to it.

int x = 10;
int *y = &x;

此处变量 "y" 存储 "x" 的地址,因此 "y" 充当 "x" 的指针。若要通过指针访问 "x" 的值,请使用解引用运算符 (*)。

Here the variable "y" stores the address of "x", hence "y" acts as a pointer to "x". To access the value of "x" with the help of its pointer, use the dereference operator (*).

Example 1

请看以下示例:

Take a look at the following example −

#include <stdio.h>

int main(){

   int x = 10;
   int *y = &x;

   printf ("x: %d Address of x: %d\n", x, &x);
   printf("Value at x with Dereference: %d", *y);

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

x: 10 Address of x: 6422036
Value at x with Dereference: 10

Example 2

你还可以使用解引用指针为原始变量分配一个值 −

You can also assign a value to the original variable with the help of the dereference pointer −

#include <stdio.h>

int main(){

   int x = 10;

   int *y = &x;

   printf("x: %d Address of x %d\n", x, &x);

   *y = 20;

   printf("x: %d with Dereference: %d", x, *y);

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

x: 10 Address of x: 6422036
x: 20 with dereference: 20

The Logical NOT Operator (!) in C

C 中的逻辑 NOT 运算符 (!) 否定布尔操作数的值。True 变成 False,False 变成 True。逻辑 NOT 运算符 (!) 是一个一元运算符。

The logical NOT operator (!) in C negates the value of a Boolean operand. True becomes False and False becomes True. The logical NOT operator (!) is a unary operator.

Example 1

下面的示例显示了在 C 中使用逻辑运算符的方法 −

The following example shows the usage of logical operators in C −

#include <stdio.h>

int main(){

   int a = 0;
   int b = 20;

   if (!(a && b)){
      printf("Line 1 - Condition is true\n" );
   }

   return 0;
}
Line 1 - Condition is true

Example 2

以下 C 代码在 while 循环中使用 NOT 运算符 −

The following C code employs the NOT operator in a while loop −

#include <stdio.h>

int main(){

   int i = 0;

   while (!(i > 5)){
      printf("i = %d\n", i);
      i++;
   }

   return 0;
}

在此代码中, while 循环会一直迭代,直到表达式 "!(i > 5)" 变成 False,而该表达式会在 "i" 的值大于 5 时变为 False。

In this code, the while loop continues to iterate till the expression "!(i > 5)" becomes False, which will be when the value of "i" becomes more than 5.

i = 0
i = 1
i = 2
i = 3
i = 4
i = 5

The 1’s Complement Operator (~) in C

C 中的 1 的补码运算符 (~) 是一个一元运算符,只使用一个操作数。运算符 "1" 的补码运算符的效果是"翻转"二进制表示中数字的各位,即 1 用 0 替换,0 用 1 替换。

The 1’s complement operator (~) in C is a unary operator, needing just one operand. It has the effect of "flipping" the bits, which means the 1’s are replaced by 0’s and vice versa in the binary representation of any number.

a

~a

0

1

1

0

假设 int 变量 "a" 的值为 60(相当于二进制中的 0011 1100),那么 "~a" 运算根据其对应位元的位元右移,以二进制补码形式得到 -61。

Assuming that the int variable "a" has the value 60 (equivalent to 0011 1100 in binary), the "~a" operation results in -61 in 2’s complement form, as per the bitwise right-shift of its corresponding bits.

~ 0011 1100 = 1100 0011

二进制数 "1100 0011" 在十进制中对应于 -61。

The binary number "1100 0011" corresponds to -61 in decimal.

Example

请看此示例代码 −

Take a look at this example code −

#include <stdio.h>

int main(){

   int a = 60;	/* 60 = 0011 1100 */
   int c = 0;

   c = ~a;      /* -61 = 1100 0011 */

   printf("Value of c is %d \n", c);

   return 0;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

Value of c is -61