I have always been confused about the meaning of "Associativity" in C Language Operator Precedence chart. See C Language Operator Precedence Chart.
Let's take, for example Ternary Operator ?: ... Let's say I have a+b ? c+d : e+f
I know what the expression means but what does the associativity of "right to left" tell me?
Meaning of "Associativity" in C Language Operator Precedence chart
Started by eddyq, Jan 20 2012 09:43 AM
10 replies to this topic
#1
Posted 20 January 2012 - 09:43 AM
|
|
|
#2
Posted 20 January 2012 - 11:11 AM
That's the way that it processes the statement, in lieu of parenthesis.
Operator associativity - Wikipedia, the free encyclopedia
Operator associativity - Wikipedia, the free encyclopedia
#3
Posted 20 January 2012 - 02:29 PM
In terms of the example I gave, does it mean it evaluates e+f then c+d then a+b?
#4
Posted 20 January 2012 - 03:29 PM
yes, it determines the prerequisites before making the determination which it needs.
Think if it in terms of a fork in the road. Once you make a determination which way to go, you don't want to have to then get the road. It should already be there.
Think if it in terms of a fork in the road. Once you make a determination which way to go, you don't want to have to then get the road. It should already be there.
#5
Posted 20 January 2012 - 03:52 PM
I see .... it seems to me that K&R got the & ^ | in the wrong place. They should be up with the * / %. That way you could code if (a & b == c | d) instead of if ((a & b) == (c | d)) which would be consistent with if (a * b == c / d).
Edited by eddyq, 20 January 2012 - 04:25 PM.
#6
Posted 21 January 2012 - 08:06 AM
I think in this case:
a+b ? c+d : e+fa+b gets evaluated first and then, depending if it's true or false, c+d or e+f gets executed.
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#7
Posted 21 January 2012 - 11:56 AM
eddyq said:
I have always been confused about the meaning of "Associativity" in C Language Operator Precedence chart. See C Language Operator Precedence Chart.
Let's take, for example Ternary Operator ?: ... Let's say I have a+b ? c+d : e+f
I know what the expression means but what does the associativity of "right to left" tell me?
Let's take, for example Ternary Operator ?: ... Let's say I have a+b ? c+d : e+f
I know what the expression means but what does the associativity of "right to left" tell me?
I guess you have used a complex problem and original context got mixed up. Here is a simple and basic rule.
Associativity is between multiple occurrences of SAME operator i.e. if a+b+c whether b+c would happen first (right to left) or a+b happens first (right to left). So it is not fair to apply ONLY this explanation to above problem i.e. conditional operator.
Conditional operator always evaluates the expression BEFORE ? first. If it is true it will only evaluate the true part i.e. before colon. Else it would evaluate and use the false part. It is important to understand that e+f will never happen if a+b results in true
You can test this using following code
int a = 4; int b = 1; int c= 2; a<5 ? b=6:c++; cout << a << b << c << endl;
c would not change in the output. So there is no question of associativity here. Because it only comes in when you have SAME operator in multiple occurrences together without any other preference.
In case of conditional op (?) example above it already mandates what is evaluated first or last.
Today is the first day of the rest of my life
#8
Posted 25 January 2012 - 03:49 PM
That is how I have always assumed it would work. But what I'm bringing up is what the precedence chart says. And that says ?: "Associativity" is "right to left" and my question here is basically ... "what does that mean because it doesn't seem to be how it operates".
#9
Posted 26 January 2012 - 12:57 AM
eddyq said:
That is how I have always assumed it would work. But what I'm bringing up is what the precedence chart says. And that says ?: "Associativity" is "right to left" and my question here is basically ... "what does that mean because it doesn't seem to be how it operates".
Associativity is right to left means "If you have multiple instances of ?: such as a > b ? b>c ? b:c : a ", then b>c with conditional is evaluated first compared to the one on the left.
That is what i explained above in paragraph
"Associativity is .... conditional operator".
Let me elaborate again.
Associativity can ONLY be applied when you have SAME operator appearing multiple times. ONLY then you can say why it is not right to left. The associativity principle DOES NOT APPLY to conditional operator in the scenario you shown above BECAUSE THERE ARE DIFFERENT OPERATORS PRESENT.
Even with ordinary operators such as *, /, + associativity DOES NOT apply until you have a situation with ONLY a single operator present OR you have multiple operators with SAME precedence. AS LONG AS PRECEDENCE can be applied, it IS USED. Associativity only comes after that.
Edited by fayyazlodhi, 26 January 2012 - 01:12 AM.
Adding e.g.
Today is the first day of the rest of my life
#10
Posted 26 January 2012 - 12:21 PM
Ok, now I get it. Thanks
#11
Posted 26 January 2012 - 06:23 PM
>>Associativity is right to left means "If you have multiple instances of ?: such as a > b ? b>c ? b:c : a ", then b>c with conditional is evaluated first compared to the one on the left.
I ran the following test: test 1 is "a>b" and test 2 is "b>c". It looks like the b>c is not evaluated first.
printf("test 1\n") ? printf("test 2\n") ? printf("case 1\n") : printf("case 2\n") : printf("case 3\n");
test 1
test 2
case 1
I ran the following test: test 1 is "a>b" and test 2 is "b>c". It looks like the b>c is not evaluated first.
printf("test 1\n") ? printf("test 2\n") ? printf("case 1\n") : printf("case 2\n") : printf("case 3\n");
test 1
test 2
case 1
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









