Precedence and Associativity of operators in C
Notes:
Precedence & Associativity of Operators in C programming language:
Precedence:
While evaluating a given expression; which operator should be evaluated first before the other?
I.e. which operator needs to be given higher precedence than the other?
Associativity:
If same operator appears two or more times in an expression, in which direction the specific operator(s) needs to be evaluated
a = 3 * 5 + 10 / 2 + 3 + 20 -10
Example code:
#include <stdio.h>
int main()
{
printf("%d\n", 2 + 2 + 2); // 6
printf("%d\n", 2 + 3 * 5); // 17 not 20
printf("%d\n", 3 * 5 + 4 / 2 + 3); // 20
printf("%f\n", 3 * 9 / 2 + 3); // 0.000000
printf("%d\n", 3 * 9 / 2 + 3); // 16
printf("%.1f\n", 3 * 9 / (float)2 + 3); // 16.5
return 0;
}