C language ke har operator ke sath ek precedence associated hoti hai. Precedence ka matlab hota hai ki ek expression jisme ek se zyada operator hai wo kese evaluate hogi. C mein bahut distinct levels ki precedence hoti hai aur har ek operator kisi na kisi precedence se belong karta hai. Higher level operators pehle evaluate hote hai. Same precedence ke operators evaluate hote hai 'left to right' or from 'right to left' depend karta hai operator ke level par aur isko ham kehte hai associativity property. Neeche wali table mein ham dekh sakte hai ki Rank 1 indicates highest precedence level and 15 the lowest.
Operators ki precedence and associativity bahut zaruri hai kisi bhi expression ko understand karne ke liye for example consider following conditional statement:
if(x == 10 + 15 && y < 10)
The precedence rules kehta hai ki addition operator ki higher priority hai logical operator se (&&) aur relational operators (== and < ). To isliye pehle 10 aur 15 ka addition execute hoga. This is equivalent to: if(x == 25 && y < 10)
Agla step ye hai ki kiya x is equal to 25 and y is less than 10. Agar ham assume kare ki x is 20 and y is 5, then
x == 25 is FALSE(0)
y < 10 is TRUE(1)
Yaha par <
operator ki higher priority hai compared to ==
, pehle y < 10
test hoga then x == 25
test hoga. Finally we get: if(FALSE && TRUE)
Ab yaha par ek condition FALSE hai, aur is case mein && (AND), yeh guaranteed hai ki agar pehle operand zero hai to second operand evaluate hi nhi hoga aur || (OR) ke case mein , second operand kabhi evaluate hi nhi hoga agar first non-zero hai.
Table: Precedence and Associativity of Operators.
Rules of precedence and associativity
- Precedence rules decides karta hai ki operators kis order mein applied honge.
- Associativity rule decides karta hai ki agar ek expression mein bahut sare same level operator hai to wo kaise applied honge.
Comments