Cprogramming 简明教程
Assignment Operators in C
在 C 语言中,赋值运算符将某个值存储在已声明的变量中。C 语言中的一个变量可以被赋予一个字面量、另一个变量或一个表达式的形式。
In C language, the assignment operator stores a certain value in an already declared variable. A variable in C can be assigned the value in the form of a literal, another variable, or an expression.
待赋值的值作为右侧操作数,而要赋值的变量应为“ = ”符号左侧的操作数,该符号在 C 中定义为一个简单的赋值运算符。
The value to be assigned forms the right-hand operand, whereas the variable to be assigned should be the operand to the left of the "=" symbol, which is defined as a simple assignment operator in C.
此外,C 有几个补充赋值运算符。
In addition, C has several augmented assignment operators.
下表列出了 C 语言支持的赋值运算符,
The following table lists the assignment operators supported by the C language −
Operator |
Description |
Example |
= |
Simple assignment operator. Assigns values from right side operands to left side operand |
C = A + B will assign the value of A + B to C |
+= |
Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. |
C += A is equivalent to C = C + A |
-= |
Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. |
C -= A is equivalent to C = C - A |
*= |
Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand. |
C *= A is equivalent to C = C * A |
/= |
Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand. |
C /= A is equivalent to C = C / A |
%= |
Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. |
C %= A is equivalent to C = C % A |
<⇐ |
Left shift AND assignment operator. |
C <⇐ 2 is same as C = C << 2 |
>>= |
Right shift AND assignment operator. |
C >>= 2 is same as C = C >> 2 |
&= |
Bitwise AND assignment operator. |
C &= 2 is same as C = C & 2 |
^= |
Bitwise exclusive OR and assignment operator. |
C ^= 2 is same as C = C ^ 2 |
= |
Bitwise inclusive OR and assignment operator. |
|
C |
= 2 is same as C = C |
2 |
Simple Assignment Operator (=)
= 运算符是 C 中使用最频繁的运算符之一。根据 ANSI C 标准,所有变量都必须一开始就声明。不允许在第一个处理语句之后声明变量。
The = operator is one of the most frequently used operators in C. As per the ANSI C standard, all the variables must be declared in the beginning. Variable declaration after the first processing statement is not allowed.
您可以在代码后期声明一个变量以赋值,或者您可以在声明时初始化它。
You can declare a variable to be assigned a value later in the code, or you can initialize it at the time of declaration.
您可以在赋值语句中使用字面量、另一个变量或表达式。
You can use a literal, another variable, or an expression in the assignment statement.
int x = 10; // declaration with initialization
int y; // declaration
y = 20; // assignment later
int z = x + y; // assign an expression
int d = 3, f = 5; // definition and initializing d and f.
char x = 'x'; // the variable x has the value 'x'.
一旦声明了某个类型的变量,就不能给它赋值任何其他类型的。在这种情况下,C 编译器会报告类型不匹配错误。
Once a variable of a certain type is declared, it cannot be assigned a value of any other type. In such a case the C compiler reports a type mismatch error.
在 C 中,引用内存位置的表达式称为“左值”表达式。左值可以作为赋值语句的左侧或右侧。
In C, the expressions that refer to a memory location are called "lvalue" expressions. A lvalue may appear as either the left-hand or right-hand side of an assignment.
另一方面,术语右值是指存储在内存中某个地址的数据值。右值是一个不能对其赋值的表达式,这意味着右值可以出现在赋值语句的右侧,但不能出现在其左侧。
On the other hand, the term rvalue refers to a data value that is stored at some address in memory. A rvalue is an expression that cannot have a value assigned to it which means an rvalue may appear on the right-hand side but not on the left-hand side of an assignment.
变量是左值,因此它们可能出现在赋值的左手。数字文字是右值,因此不能被赋值,不能出现在赋值的左手。看一看以下有效和无效的声明 −
Variables are lvalues and so they may appear on the left-hand side of an assignment. Numeric literals are rvalues and so they may not be assigned and cannot appear on the left-hand side. Take a look at the following valid and invalid statements −
int g = 20; // valid statement
10 = 20; // invalid statement
Augmented Assignment Operators
除了 = 运算符,C 允许您将算术运算符和按位运算符与 = 符号组合起来,以形成增强赋值运算符或复合赋值运算符。增强运算符为将算术运算或按位运算与赋值相结合提供了一个方便的捷径。
In addition to the = operator, C allows you to combine arithmetic and bitwise operators with the = symbol to form augmented or compound assignment operator. The augmented operators offer a convenient shortcut for combining arithmetic or bitwise operation with assignment.
Example 1
例如,表达式“a += b”的效果与先执行“a + b”然后将结果重新赋值给变量“a”相同。
For example, the expression "a += b" has the same effect of performing "a + b" first and then assigning the result back to the variable "a".
#include <stdio.h>
int main(){
int a = 10;
int b = 20;
a += b;
printf("a: %d", a);
return 0;
}
运行代码并检查其输出:
Run the code and check its output −
a: 30
Example 2
类似地,表达式“a <⇐ b”的效果与先执行“a << b”然后将结果重新赋值给变量“a”相同。
Similarly, the expression "a <⇐ b" has the same effect of performing "a << b" first and then assigning the result back to the variable "a".
#include <stdio.h>
int main(){
int a = 60;
int b = 2;
a <<= b;
printf("a: %d", a);
return 0;
}
运行代码并检查其输出:
Run the code and check its output −
a: 240
Example 3
以下是演示在 C 中使用赋值运算符的 C 程序:
Here is a C program that demonstrates the use of assignment operators in C −
#include <stdio.h>
int main(){
int a = 21;
int c ;
c = a;
printf("Line 1 - = Operator Example, Value of c = %d\n", c );
c += a;
printf("Line 2 - += Operator Example, Value of c = %d\n", c );
c -= a;
printf("Line 3 - -= Operator Example, Value of c = %d\n", c );
c *= a;
printf("Line 4 - *= Operator Example, Value of c = %d\n", c );
c /= a;
printf("Line 5 - /= Operator Example, Value of c = %d\n", c );
c = 200;
c %= a;
printf("Line 6 - %%= Operator Example, Value of c = %d\n", c );
c <<= 2;
printf("Line 7 - <<= Operator Example, Value of c = %d\n", c );
c >>= 2;
printf("Line 8 - >>= Operator Example, Value of c = %d\n", c );
c &= 2;
printf("Line 9 - &= Operator Example, Value of c = %d\n", c );
c ^= 2;
printf("Line 10 - ^= Operator Example, Value of c = %d\n", c );
c |= 2;
printf("Line 11 - |= Operator Example, Value of c = %d\n", c );
return 0;
}
编译并执行上述程序时,将生成以下结果:
When you compile and execute the above program, it will produce the following result −
Line 1 - = Operator Example, Value of c = 21
Line 2 - += Operator Example, Value of c = 42
Line 3 - -= Operator Example, Value of c = 21
Line 4 - *= Operator Example, Value of c = 441
Line 5 - /= Operator Example, Value of c = 21
Line 6 - %= Operator Example, Value of c = 11
Line 7 - <<= Operator Example, Value of c = 44
Line 8 - >>= Operator Example, Value of c = 11
Line 9 - &= Operator Example, Value of c = 2
Line 10 - ^= Operator Example, Value of c = 0
Line 11 - |= Operator Example, Value of c = 2