Questions tagged [associativity]

Anything related to notational associativity of binary operators. The associativity of operators relates to the order of evaluation of subexpressions in an expression where operators having the same precedence are used repeatedly, such as in `a+b+c-d`.

Anything related to notational associativity of binary operators. The associativity of operators relates to the order of evaluation of subexpressions in an expression where operators having the same precedence are used repeatedly, such as in a+b+c-d.

See the Wikipedia page on operator associativity in programming languages.

Note: the concept is related to the associative property of operators in mathematics, but there are some subtle differences. Compare with Wikipedia page on associative property.

A common problem when first learning to parse infix operators using recersive descent is that one will get expressions such as 1+2 or 3-1 or 3*4 or 6/3 to work. Then they will try using multiple sequential of an operator such 1+2+3 which will work but find that 7-3-2 which is equivalent to (7-3)-2 which is 2 will not work and typically get 6 because they have parsed it as 7-(3-2) which is incorrect.

For solutions to this problem
See: Parsing Expressions by Recursive Descent by Theodore Norvell

185 questions
-2
votes
1 answer

In C programming Language: What order would 3 (Number 3) be assigned to the variables? As in which variable would receive 3 first, second and third?

Question Continued: In C programming Language: For the Question below: What order would 3 (Number 3) be assigned to the variables? As in which variable would receive 3 first, second and third? And which variable would have 3 in the end? Question: A…
-2
votes
1 answer

Explain output of following C program

#include main() { int a=1; printf("%d %d %d %d %d\n",++a,a++,++a,++a,a++); a=1; printf("%d %d %d %d %d",a,a++,a,++a,a); } When I run it , it gives following output. 6 4 6 6 1 3 2 3 3 3 Please explain the code.
-3
votes
3 answers

How to make the C++ compiler to follow the same precedence, associativity and order of evaluation that C# has in the this assignment statement?

Consider the following piece of code: int x = 1; int y = 2; y = x + (x = y); When this runs in C#, the variables end up assigned with these values: x = 2 y = 3 On the other hand, when the same runs in C++, the variables end like this: x = 2 y =…
Lesair Valmont
  • 796
  • 1
  • 8
  • 15
-3
votes
1 answer

Please explain the output of this simple C program

int i = 2, j = 3, k, l ; float a, b ; k = i / j * j ; l = j / i * i ; a = i / j * j ; b = j / i * i ; printf( "%d %d %f %f", k, l, a, b ) ; } it is a simple c program from yashwant kanetkar but i could not relate to the answer . if we compile this…
-6
votes
1 answer

Operator precedence of |, ~ and & operator?

I'm confused about these logical operators. can someone please explain the precedence and associative rules of these operators. in bit wise operations, a=011, b=010 and c=001 in d whether a should be negated first or should the evaluation be started…
1 2 3
12
13