"Doing mistakes while learning/practicing is better than doing mistakes while doing real projects" Isn't it ?
Today I was practicing the basics of programming language "C" and I found that I wasn't able to do simple mathematical operation (+,-,*,?). I studied the same again and henceforth I'm sharing it here so that readers of this post can strengthen their skills in associativity of operators.
Before reading ahead I would like you to answer the below mentioned question honestly as a comment.
It's always better to be a student i.e. open to learn new things.
Have a happy programming !
Today I was practicing the basics of programming language "C" and I found that I wasn't able to do simple mathematical operation (+,-,*,?). I studied the same again and henceforth I'm sharing it here so that readers of this post can strengthen their skills in associativity of operators.
Before reading ahead I would like you to answer the below mentioned question honestly as a comment.
What will be the output of :
- 3/2*5
- 3*5/2
Associativity of Operators
The hierarchy of commonly used operators in programming
language “C” is shown in the below table.
Priority
|
Operators
|
Description
|
1st
|
* / %
|
Multiplication, Division, Modular Division
|
2nd
|
+ -
|
Addition, Subtraction
|
3rd
|
=
|
Assignment
|
I’m giving an example here which will let you understand the
need of studying associativity of operators.
Example : 3/2*5
As explained in the above mentioned table, operators / and *
have same priority and hence which part of this expression is to be executed
first ?
Option 1
|
Option 2
|
3/2*5
|
3/2*5
|
1*5
|
3/10
|
5
|
0
|
Note : 3/2 is equal to1 because by default the system will treat
it as an integer. Even though we have BODMAS concept in mathematics but in case
of programming language, it’s very important to understand the concept of
associativity.
Question : Which option is correct Option 1 or Option 2 ?
Answer : It will be executed according to the associativity
of operators.
Explanation : Associativity of Operators
Reference : Download Appendix A – Precedence Table from
internet or refer Page No. 692 of Let Us C (Seventh Edition) by Yashavant P.
Kanetkar.
Note : Ambiguous means “having more then one meaning.”
According to the Appendix A both operators / and * have left
to right associativity and according to the table mentioned below only /
operator has unambiguous left operand and henceforth operator / is performed
earlier.
Operator
|
Left
|
Right
|
Remark
|
/
|
3
|
2 or
2*5 |
Left operand is unambiguous.
(not having double meaning) |
*
|
3/2 or
2 |
5
|
Right operand is unambiguous.
(not having double meaning)
|
Even though the same example is mentioned in Let Us C book
at page no. 36 but I’ve tried to glow more lights on it so that one can understand associativity in a better way and I’m confident by referring it, you’ll be able
to do stepwise evaluation of the below mentioned expressions.
Assume H to be an integer
H=a*b-c*d
H=3/2*4+3/8+3
H=2*3/4+4/4+8-2+5/8
H=4*1/2+3/2*1+2+3.2
H=4*2/4-6/2+2/3*6/2
H=1/3*4/4-6/2+2/3*6/3
Assume H to be float
H=2/2+2*4/2-2+2.5/3
It's always better to be a student i.e. open to learn new things.
Have a happy programming !