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 मध्ये प्रत्येक चॅप्टरच्या खाली केली आहे.  

Friday 22 November 2013

if Decision Control Structure in C

Decision Control Structures मध्ये एकुण ४ प्रकारची control structures आहेत. अर्थात control म्हणजे काय हे माहीत हवे. Program execute होतांना प्रत्येक स्टेटमेंट हे compiler कडुन compile केले जाते. मात्र execute करण्यासाठी OS कडुन व CPU कडुन मदत घ्यावी लागते. पण हि सर्व स्टेटमेंट कोणत्या order ने execute होतात हे महत्वाचे. 

जर program मध्ये लिहीलेली सर्व च्या सर्व स्टेटमेंट विनासायास एका मागोमाग execute होत असतील तर त्याला Sequential Control Structure म्हणतात. 

पण जर एखादे स्टेटमेंट किंवा काही स्टेटमेंट्स execute करायची कि नाही हे जर काही conditions satisfy झाल्या तरच ठरवायचे असेल तर लिहीलेल्या स्टेटमेंट्स ना अथवा ब्लॉक ला Decision Control Structure म्हणतात. 

C language मध्ये decision घेण्यासाठी ३ पद्धती आहेत. 


  • पहीली म्हणजे if control structure
  • दुसरी म्हणजे if else control structure
  • तिसरी म्हणजे case control structure


if control structure

syntax:

if(condition)
{
do this;
do this; 
}

या control structure मध्ये if हा keyword आहे. या keyword नंतर लगेच pair of round brackets मध्ये condition लिहीली जाते. 


condition लिहीण्यासाठी मात्र C language मधील 6 relational operators वापरता येतात ते म्हणजे 

  1. less than (<) 
  2. Less than or equal to (<=)
  3. Greater than (>)
  4. Greater than or equal to (>=)
  5. Equal to (==)  आणी 
  6. Not equal to (!=)


वरुन operators वरून हे कळतेच की हे सर्व relational operators Binary Operators आहेत. प्रोग्रॅम लिहीतांना assignment operator (=) आणी equal to operator (==) मध्ये गोंधळ होण्याचीई शक्यता असते. या ठिकाणी equal to operator हा relational operator आहे हे लक्षात घेतले पाहीजे. 

वरील syntax मध्ये हे लक्षात घेतले पाहीजे की condition हि दोन operands मध्ये टेस्ट केली जाते. व या expression चा result एकतर True (non-zero म्हणजेच 1) किंवा False (Zero म्हणजेच 0) असतो. 

ज्यावेळी result True असेल त्याच वेळी if condition शी associated statements execute केली जातात. 
जर result False असेल तर if block किंवा if condition शी associated statements execute केली जाणार नाहीत. 

interview किंवा viva मध्ये तुम्हाला 

if(x)

असे स्टेटमेंट दिसून येइल. या ठिकाणी relational operator वापरलेला नाही. पण वर सांगीतल्या प्रमाणे round brackets च्या आत असलेला x हे expression म्हणून सोडवले जाते. आता x मध्ये non-zero value असली तर ते true असेल व x मध्ये 0 असेल तर expression false होइल. 

Click here if (condition) च्या व्हिडीओ साठी...! 


Do you like the C Marathi e-learning concept?