优先级

33 阅读1分钟

1、优先级最高:()

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

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

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

示例

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

运行结果 image.png

int x = 6;
int res = -x + 4;
printf("%d\n", res);

结果 image.png

int a = 5;
int res = a++ * 4;
printf("%d\n", res);

结果

image.png

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

结果

image.png

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

结果

image.png

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

结果

image.png

#include <stdio.h>

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

    return 0;
}

image.png

Snipaste_2025-10-23_11-27-55.png