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

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

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

下表是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

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页