|
C Programming Reference >> C Programming Reference Question Bank >> 19/07/07 - 475 Views - Ratings : Question - State the output of the following code.
Answer - c = 3 d = 5 Explanation - This output seem strange but will definately make sense to you if you take into account the operator priority. In general assigment operator has higer priority than comma operator. Comma operator is used to concatenate two operation into one and the entire expression returns value of the right most operation while all operation on the left are term as void. Now in expression c=a,b; observe that no assigment of value returned by expression takes place. It is parsed by compiler in a default way which is same as (c=a),b; . First operation puts value of a in c ie. 3 and second returns the value of b which is discarded as we are not storing it. But in second expression the parenthesis changes the order of parsing. Variable 'd' here is assigned value returned by right most comma operation ie of b (5). Also in this case value of a is discarded because its not the rightmost operation. It is important to note that priority of operators result in entirely different outcomes which are difficult to predict and easy to miss. Reader Comments -
|
|||||||