运算符的优先级决定了表达式中运算执行的先后顺序,优先级高的运算符最先被执行.
C语言运算符的优先级和关联性如下:
创建源文件:moperators_precedence.c ,其代码如下:
#include <stdio.h>
void main() {
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Line 1 - Value of (a + b) * c / d is : %dn", e);
e = ((a + b) * c) / d; // (30 * 15 ) / 5
printf("Line 2 - Value of ((a + b) * c) / d is : %dn", e);
e = (a + b) * (c / d); // (30) * (15/5)
printf("Line 3 - Value of (a + b) * (c / d) is : %dn", e);
e = a + (b * c) / d; // 20 + (150/5)
printf("Line 4 - Value of a + (b * c) / d is : %dn", e); return 0; }
执行代码,得到如下结果:
Line 1 - Value of (a + b) * c / d is : 90
Line 2 - Value of ((a + b) * c) / d is : 90
Line 3 - Value of (a + b) * (c / d) is : 90
Line 4 - Value of a + (b * c) / d is : 50