search
Log In
1 vote
675 views

State True/False: "If two operators in an expression have equal precedence but different associativity then order of their evaluation is compiler dependent."

Considering my adaptation to this question: 
if * and ^(exponentiation) has same precedence

if *  associate from right to left and ^ associates from left to right,   then what will be the result of 2*2^3*4 ? 

in Programming
edited by
675 views
0
Can we have more discussion on this question?

3 Answers

5 votes

Answer is false. But the question is bad because both precedence and associativity do not determine the evaluation order in languages like C/C++ where as they do in many other languages.

Example: a + b + c;

Here associativity is left to right (assumed) and hence (a+b) happens first and then + c.

But consider (a + b) + (c + d) + (e+d);

Here (a+b) or (c+d) or (e+d) can happen first.

For the question being adapted, that is never possible. Because all operators having same precedence must have same associativity - because that is the purpose of associativity. See here for example the precedence table in C:

http://ee.hawaii.edu/~tep/EE160/Book/chap5/subsection2.1.4.1.html
 


edited by
0

@Arjun Sir I think if the expression is (a + b) + (c + d) + (e+d); then (a+b) will happen first and then (c + d) and then (e+d) because () is also an operator and has the highest precedence with left-right associativity.

Is my below inference correct -

If two operators have same precedence then associativity will be used to resolve the tie and in case if associativity is also same then maybe it is ambiguous and becomes compiler dependent on which one to evaluate first.

0
this is one of the previous years GATE Questions. So either u r wrong or the GATE authorities might have set a vague question.
0
Which year? I do not think this came in any previous GATE paper.
0
@yash operator precedence and associaitivty just makes a grouping of operators. i.e., they determine the operands for each operator. Final execution order is not determined by this - this is done to ensure more flexibility for compilers for performance reasons.
0

@Arjun Sir I have the below doubts -

 For ex, if an expression would have been

y = add(a, b ) + sub( c, d ) + div( e/f );

where add, sub or div are functions. The order becomes compiler dependent as which function to invoke first is a choice.

But if  expression is  (a + b) + (c + d) + (e+d).

After the grammar creates a syntax tree and converts it into postifix notation using SDT.

the expression would be come = ab+ + cd+ + ed+ 

                                                  ab+cd++  + ed+ 

                                                  = ab+cd++ed++

The order of evaluation becomes fixed in this case isnt it?

0 votes

Hope this video may help you 

0 votes

Trick to remeber the precedence table

https://youtu.be/Du3bSxTi_tw

Related questions

0 votes
0 answers
1
200 views
#include <stdio.h> int main(){ int val=10; int c= val+ ~val + ++val; printf("%d %d",c,val); } Output is 10 11 #include <stdio.h> int main(){ int val=10; int c= val + (~val + ++val); printf("%d %d",c,val); } output is 11 11 #include <stdio.h> int main(){ int val=10; int c=(val) + (~val + ++val); printf("%d %d",c,val); } output is 11 11 What is the difference?
asked May 20, 2018 in Programming SreenivasaRaju 200 views
0 votes
2 answers
2
691 views
Hi, Is there any smart way to learn operator precedence table ? (means can we form some kind of logical connection instead of mugging entire table.) If some good reference could be provided then it will be great help. PS: http://www.geeksforgeeks.org/c-operator-precedence-associativity/
asked Nov 20, 2017 in Programming Chhotu 691 views
0 votes
1 answer
3
0 votes
1 answer
4
1.1k views
int main(){ int a = 4,b = 5,c = 6; int k = ++a || ++b && c++; printf("%d %d %d %d",a,b,c,k); return 0; } o/p 5 5 6 1 Above code will not increment b and c why??even the precedence of logical And is more than logical Or
asked Feb 3, 2016 in Programming indrajeet 1.1k views
...