运算符的优先级

87 阅读1分钟

运算符的优先级

1、优先级最高:()

2、单目运算符:(如‘!’‘++’‘’)> 双目(如‘+’‘’)

3、逻辑运算符:&& > ||

4、赋值运算符最后运行,如‘=’‘>=''<='

下为示范:

int a = 3, b = 4, c = 5;
int res = a + b * c;
printf("%d\n", res);//23

image.png

int x = 5;
int res = -x + 3;
printf("%d\n", res);//-2

image.png

int a = 2;
int res = a++ * 3;
printf("%d\n", res);//6

image.png

int a = 3, b = 5;
int res = a > b && a < b; 
printf("%d\n", res);//0

image.png

int x = 10;
x += x * 2;
printf("%d\n", x);//30

image.png

int a = 6, b = 9, c = 8;
int res = (a + c) * (b - a);
printf("%d\n", res);//42

image.png

运用:判断是不是闰年

int year;
printf("请输入一个年份:");
scanf("%d", &year);
		
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) ?
printf("%d 年是闰年\n", year) : printf("%d 年不是闰年\n", year); 

image.png image.png