简单的赋值运算符由等号(=)实现,只是把等号右边的值赋予等号左边的变量。

复合赋值运算是由乘性运算符、加性运算符或位移运算符加等号(=)实现的。

下表是C语言支持的赋值运算符

c语言中赋值运算符与算术运算符(C语言运算符之赋值运算符)(1)

创建源文件:massignment_operators.c ,其代码如下:

#include <stdio.h>

void 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 );

}

执行代码,得到如下结果:

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

,