Jump to content

Meaning of "Associativity" in C Language Operator Precedence chart

- - - - -

  • Please log in to reply
10 replies to this topic

#1
eddyq

eddyq

    Newbie

  • Members
  • Pip
  • 6 posts
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?

#2
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
That's the way that it processes the statement, in lieu of parenthesis.

Operator associativity - Wikipedia, the free encyclopedia

#3
eddyq

eddyq

    Newbie

  • Members
  • Pip
  • 6 posts
In terms of the example I gave, does it mean it evaluates e+f then c+d then a+b?

#4
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
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.

#5
eddyq

eddyq

    Newbie

  • Members
  • Pip
  • 6 posts
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
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I think in this case:
a+b ? c+d : e+f
a+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
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts

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?

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
eddyq

eddyq

    Newbie

  • Members
  • Pip
  • 6 posts
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
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts

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
eddyq

eddyq

    Newbie

  • Members
  • Pip
  • 6 posts
Ok, now I get it. Thanks

#11
eddyq

eddyq

    Newbie

  • Members
  • Pip
  • 6 posts
>>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




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users