C Programming - Expressions - Discussion

Discussion Forum : Expressions - True / False Questions (Q.No. 1)
1.
Associativity has no role to play unless the precedence of operator is same.
True
False
Answer: Option
Explanation:

Associativity is only needed when the operators in an expression have the same precedence. Usually + and - have the same precedence.

Consider the expression 7 - 4 + 2. The result could be either (7 - 4) + 2 = 5 or 7 - (4 + 2) = 1. The former result corresponds to the case when + and - are left-associative, the latter to when + and - are right-associative.

Usually the addition, subtraction, multiplication, and division operators are left-associative, while the exponentiation, assignment and conditional operators are right-associative. To prevent cases where operands would be associated with two operators, or no operator at all, operators with the same precedence must have the same associativity.

Discussion:
6 comments Page 1 of 1.

Anitha Devi S said:   6 years ago
Can you please explain when and where left and right associativity is used?

Lakshmipraveen said:   8 years ago
a=b=3 this has an associativity from right to left. first the value '3' is assigned to 'b' and the value of 'b' is assigned to 'a'.

Precedence or associativity is the priority of its execution(from left to right or from right to left and operators signs), depends upon the compiler.

Rushil said:   8 years ago
What about a=b=3?

Vicky said:   8 years ago
Can anyone explain what is precedence?

Janani said:   8 years ago
If two operators have the same precedence, then the order in which they are evaluated is associativity.

For example:((1 == 2) != 3)
i.e, (1 == 2) executes first resulting into 0 (false)
then, (0 != 3) executes resulting into 1 (true).

Vikranth said:   9 years ago
What is associativity can anyone explain it? Please.

Post your comments here:

Your comments will be displayed after verification.