C语言中的运算符:
算术运算符:加(+)、减(—)、乘()、除(/)、取余(%)
关系运算符:>, >=, <, <=, !=,
逻辑运算符:!(非),&&(且),||(或)
赋值运算符:=, +=,=, /=
例如:a+=3是等价于a=a+3,a/=3等价于a=a/3
其优先级别是算术>关系>逻辑>赋值。
示例程序1:
#include <stdio.h>
int main(void)
{
int i = 10;
int k = 20;
int m;
m = (3>2) && (k=8);
printf("m = %d , k = %d\n",m,k);
return 0;
}
运行结果:
m = 1 , k = 8
Press any key to continue
实例程序2:
#include <stdio.h>
int main(void)
{
int i = 10;
int k = 20;
int m;
m = (3>5) && (k=8); //此时k=8将不会被执行
printf("m = %d , k = %d\n",m,k);
return 0;
}
运行结果:
m = 0 , k = 20
Press any key to continue
实例程序3:
#include <stdio.h>
int main(void)
{
int i = 10;
int k = 20;
int m;
m = (5>3) || (k=8); //此时k=8将不会被执行
printf("m = %d , k = %d\n",m,k);
return 0;
}
运行结果:
m = 1 , k = 20
Press any key to continue
实例程序4:
#include <stdio.h>
int main(void)
{
int i = 10;
int k = 20;
int m;
m = (5 > 8) || (k=8); //此时k=8将会执行
printf("m = %d , k = %d\n",m,k);
return 0;
}
运行结果:
m = 1 , k = 8
Press any key to continue