Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator Precedence - Expression Evaluation

For the following code snippet I get the output as 1. I want to know how it came?

void main()
{
int x=10,y=20,z=5,i;
i=x<y<z;
printf("%d",i);
}
like image 468
Babanna Duggani Avatar asked Mar 28 '11 13:03

Babanna Duggani


People also ask

Which is the highest evaluation operator precedence order?

The operator precedence is responsible for evaluating the expressions. In Java, parentheses() and Array subscript[] have the highest precedence in Java. For example, Addition and Subtraction have higher precedence than the Left shift and Right shift operators.

What is the order of evaluation of the operators?

Proceeding from left to right, it is evaluated as follows: Expressions within parentheses are evaluated first. Expressions with operators of higher priority are evaluated before expressions with operators of lower priority.

Which operator should be evaluated first in an expression?

When expressions contain operators from more than one category, arithmetic operators are evaluated first, comparison operators are evaluated next, and logical operators are evaluated last. Comparison operators all have equal precedence; that is, they are evaluated in the left-to-right order in which they appear.

What has the highest precedence and is evaluated first?

Parentheses have the highest precedence and can be used to force an expression to evaluate in the order you want. Since expressions in parentheses are evaluated first, 2 * (3-1) is 4, and (1+1)**(5-2) is 8.


1 Answers

i=x<y<z;, gets interpreted as i=(x<y)<z, which in turn gets interpreted as i=1<z, which evaluates to 1.

like image 143
yan Avatar answered Oct 05 '22 16:10

yan