Sunday 24 November 2013

Precedence and Associativity of operators in C

C language मध्ये काही concepts असे आहेत कि ज्याचा स्वतंत्रपणे अभ्यास केला जात नाही. "प्रोग्रॅम सोडवतांना अडचण येइल त्यावेळी बघून घेउ काय करायचे ते" अशी सर्व साधारण मनोवृत्ती असते. 

Operator associativity हा अशाच काही टॉपीक पैकी एक टॉपीक. आज मी या पोस्ट मध्ये Operator precedence and associativity या बद्दल सांगते.    

C language is very rich in number of operators. आणी हे सर्व operators वेगवेगळ्या categories मध्ये विभागून ठेवले आहेत. म्हणजे त्याच्या broad categories 


  1. Unary operators (Single operand)
  2. Binary operators (Two operands on both side of operator)
  3. Ternary operators (three operands) 


अशा आहेत. 

शिवाय हे सर्व 

General operators
e.g. function operator ( ), Array expression [ ], Structure operator ->, Structure operator
Unary operators
e.g. unary minus - , increment ++, decrement --, one's complement ~, Negation !, Address of &, value at address *, type cast (type), size in bytes (sizeof)   
Mathematical operators
e.g. Multiplication *, Division /, Modulus %, Addition +, Subtraction -, 
Shift operators
e.g. Left shift <<, Right shift >>
Relational operators
e.g. Less than <, Less than or equal to <= , Greater than >, Greater than or equal to >=, equal to ==, Not equal to != 
Bitwise operators
e.g. BIT wise AND &, BIT wise exclusive OR ^, BIT wise inclusive OR |
Logical operators
e.g. Logical AND &&, Logical OR ||
Ternary operators
e.g. Conditional operator ? : 
Assignment operators
e.g. =, *=, /=, %=, +=, -=, &=, ^=, |=, <<=, >>= 
Comma operator 
e.g. ,

अशा categories मध्ये विभागले गेले आहेत. 


वरील सर्व operators हे Highest precedence पासून Lowest precedence पर्यंत लिहीले आहेत. म्हणजेच Function operator ला highest precedence आहे तर comma operator ला lowest precedence आहे. 

आता precedence म्हणजे एखादे एक्सप्रेशन सोडवतांना कोणते operation प्रथम करायचे, त्या नंतर कोणते करायचे याचा जो sequence follow करायचा असतो तो... उदा. 

x = 8 / 4 + 4 % 2 

या expression मध्ये वरील चार्ट प्रमाणे / आणी % ला + operator पेक्षा higher precedence आहे तर = ला मात्र + पेक्षा सुद्धा कमी precedence आहे. म्हणजेच त्याच क्रमाने operations होतात. म्हणजेच
8/4 will be evaluated to 24%2 will be evaluated to 02 + 0 will be evaluated to 2 and will be assigned to x 
आता समजा expression 
x = 8 / 4 * 2  
असे मात्र असेल तर अनेक वेळा 4 * 2 हे प्रथम सोडवून नंतर मग 8/8 हे ऑपरेशन विद्यार्थ्यांच्याकडून केले जाते. या ठिकाणी associativity consider केली जाते. म्हणजेच एकच precedence असलेले operators जर sequentially आले तर कोणत्या मार्गाने operations करायची त्याच्या संबधीत language चे rules म्हणजे associativity...!

Associativity of operators in C हि दोन प्रकारची असते 
Left to Right (याला काहीवेळा Left associativity असे सुद्धा म्हणतात)Right to Left (याला काहीवेळा Right associativity असे सुद्धा म्हणतात)
हि associativity operators च्या संबधीत असते. म्हणजे C च्या operator precedence table मध्ये Dennis Ritchie ने precedence बरोबर associativity सुद्धा दिली आहे. उदा. mathematical operators ला Left to Right associativity आहे तर assignment operators, unary operators आणी comma operators ला Right to Left associativity आहे. 

म्हणजेच वरील expression आता associativity च्या नियमानुसार 8/4 हे प्रथम सोडवून मिळालेल्या result ला 2 ने multiply केले जाते व x मध्ये 4 store होतो. 

हे सगळं वाचल की समजल असं वाटत पण ज्या वेळी precedence chart मधील अनेक operators मात्र एखाद्या expression मध्ये उलट-सुलट येतात त्यावेळी भंबेरी उडते. अनेक छोटे-छोटे प्रोग्रॅम्स डोळ्याखालून घालणे व त्याचे execution समजावून घेणे हाच एकमेव उपाय आहे. आमच्या टिम मधील काही engineers नी अशा प्रकारच्या अनेक प्रश्नांची व त्याच्या explanation ची व्यवस्था C Marathi framework मध्ये प्रत्येक चॅप्टरच्या खाली केली आहे.  

No comments:

Post a Comment

Do you like the C Marathi e-learning concept?