Associativity and precedence in C
Hello coder! Welcome back.
In this session will consider associativity and precedence concept.
Precedence decides which operator should be evaluated first in an expression containing many operators.
Example-
Always division has higher precedence. Hence '3' is the correct answer. 3/3 is performed first and 2 is added to it. 'BODMAS' rule on that bases precedence of an operator is decided.
Associativity can be either from left to right or right to left.
Let's take an example-
Multiplication and division have the same precedence. According to associativity, evaluate from left to right. After evaluating from left to right the answer is 9.
Let's look into associativity and precedence table-
Out of all the operators mentioned in the table, will look at only important operators and discuss about them-
☆ parenthesis ( ) is used for function call i.e if you want to call any function (Function is nothing but transferring the control to another set of code outside the main function, executing those instructions and returning the result to the main function).
Let's take an example-
int a = test( );
How complier will get to know that it is a function call? It can be a variable initialization too. let's clarify.
Here assignment operator has the least precedence compared to parathesis. Hence compiler reads it as-
int a=(test( ));
After getting value from the test function, the value gets assigned to 'a' variable.
☆ Postfix increment and decrement operator-
Post increment or decrement operator has higher precedence compared to pre-increment or decrement.
Associativity of post increment or decrement operator is from left to right and associativity of pre-increment or decrement operator is from right to left.
☆ Associativity is helpful when there are two or more operators of the same precedence.
I hope so you got a clear picture of precedence and associativity in C. If any doubts drop a comment below.
Happy learning.
In this session will consider associativity and precedence concept.
What is precedence?
Consider an expression having many operators. Which operator should we evaluate first? There comes the precedence concept.Precedence decides which operator should be evaluated first in an expression containing many operators.
Example-
Always division has higher precedence. Hence '3' is the correct answer. 3/3 is performed first and 2 is added to it. 'BODMAS' rule on that bases precedence of an operator is decided.
What is associativity?
Now let's consider another expression having the same precedence. Which operator should be evaluated first? Then comes the associativity concept.
Associativity can be either from left to right or right to left.
Let's take an example-
Multiplication and division have the same precedence. According to associativity, evaluate from left to right. After evaluating from left to right the answer is 9.
Let's look into associativity and precedence table-
Out of all the operators mentioned in the table, will look at only important operators and discuss about them-
☆ parenthesis ( ) is used for function call i.e if you want to call any function (Function is nothing but transferring the control to another set of code outside the main function, executing those instructions and returning the result to the main function).
Let's take an example-
int a = test( );
How complier will get to know that it is a function call? It can be a variable initialization too. let's clarify.
Here assignment operator has the least precedence compared to parathesis. Hence compiler reads it as-
int a=(test( ));
After getting value from the test function, the value gets assigned to 'a' variable.
☆ Postfix increment and decrement operator-
Post increment or decrement operator has higher precedence compared to pre-increment or decrement.
Associativity of post increment or decrement operator is from left to right and associativity of pre-increment or decrement operator is from right to left.
☆ Associativity is helpful when there are two or more operators of the same precedence.
I hope so you got a clear picture of precedence and associativity in C. If any doubts drop a comment below.
Happy learning.
Comments
Post a Comment