i read some time ago about precedence and associativity of operators in C And i thought i have it clear but read some post here on SO and i think im stuck... I read some posts/questions, here is one of them:
Operator precedence versus order of evaluation
I know what means order of evaluation in C, for example using functions in an expression:
int x = a() + b() * c();
"The evaluation order of the 3 function calls is unspecified, i.e. the functions can be called in any order"
I got this, but what about if we have this expression:
int x;
int a = 2;
int b = 5;
int c = 6;
int d = 3;
x = a * b + c / d;
First of all, according to this post: Order of evaluation of expression here enters "order of evaluation" and then precedence and associativity right?
I mean, due to the unspecified order of evaluation in C we could have:
Step 1: x = 2 * b + c / d; ("a" is first evaluated)
Step 2: x = 2 * b + 6 / d; ("c" is then evaluated)
Step 3: x = 2 * b + 6 / 3; ("d" is now evaluated)
Step 4: x = 2 * 5 + 6 / 3; ("b" is finally evaluated)
But we could have another possibilities like this order of evaluation: b,c,d,a or d,c,a,b or any other combination right? So independant of the evaluation order, at the end we have:
x = 2 * 5 + 6 / 3;
Now then, in that post they answered:
Here it's pretty obvious that there are two sub-expressions, i.e. a * b and c / d. Due to operator precedence both of these sub-expressions must be evaluated before the addition but the order of evaluation is unspecified, i.e. we can't tell whether the multiplication or the division is done first.
This is the first thing that confuses me, i mean, for what i have read about precedence and associativity, we have " * " and "/" who have higher precedence than "+", so according to associativity for " * " and "/" which is "Right to left"
The "/" operatorion with its respectives operands should be performed first, i mean:
x = 2 * 5 + (6/3) --> x = 2 * 5 + 2
And then the " * " operation with its respective operands should be next:
x = (2 * 5) + 2 --> x = 10 + 2
And finally the addition takes place:
x = 12;
All of this according the precendece and associativity os operators in C, but in the post they, again, answered:
...we can't tell whether the multiplication or the division is done first...
But associativity (Right to left) tell us that division should come first than multiplication, but if why im sayong its incorrect, which is the purpose of associativity? it is really a thing in C language?
Lets change now the expression for this one:
x = a * b / (c + d);
Now, in the same post, another person answered about this last expression:
Given your example x = a * b / (c + d); precedence and associativity cause the expression to be parsed as (x) = ((a * b) / (c + d)) The multiplicative operators * and / have the same precedence and are left-associative, so a * b / (c + d) is parsed as (a * b) / (c + d) (as opposed to a * (b / (c + d))). So what this tells us is that the result of a * b is divided by the result of c + d, but this does not mean that a * b must be evaluated before c + d or vice versa.
Here i have another problem, "()" has higher precedence than the others operators in the expression, so c+d operation should be done first, but this person says that now we have:
(x) = ((a * b) / (c + d))
And according to associativity, for "()"is left to right, so "a*b" should be performed first, then "c+d" and finaly the division...
Sorry if you feel this question has already being answered but the more i read about other questions in SO about this topic in particular, the less i feel i know :/
And that post has some years already so i decided to ask it as a question at all
Thanks in advance! i would really appreciate your answers!