Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If parenthesis has a higher precedence then why is increment operator solved first?

I have a single line code,

int a = 10; a = ++a * ( ++a + 5); 

My expected output was 12 * (11 + 5) = 192 ,but I got 187. As much as I knew the increment operator inside () is to be solved first, then why the one outside is solved first?

like image 243
frunkad Avatar asked Jan 29 '15 16:01

frunkad


People also ask

What is the precedence of the increment operator?

Precedence of increment and decrement operators in C The increment and decrement operators have higher precedence than other operators except for parentheses. This means when an expression is evaluated the increment/decrement operations are performed before other operations.

Which operator comes first in operator precedence?

Operators are first grouped by precedence, and then, for adjacent operators that have the same precedence, by associativity. So, when mixing division and exponentiation, the exponentiation always comes before the division.

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.


2 Answers

Expressions are evaluated left to right. Parentheses (and precedence) just express grouping, they don't express ordering of evaluation.

So

 11 * (12 + 5) ++a   ++a 

equals

187 
like image 109
Sotirios Delimanolis Avatar answered Sep 18 '22 20:09

Sotirios Delimanolis


Quoting from Eric Lippert's Blog:

The evaluation of an arithmetical expression is controlled by three sets of rules: precedence rules, associativity rules, and order rules.

Precedence rules describe how an underparenthesized expression should be parenthesized when the expression mixes different kinds of operators.

Associativity rules describe how an underparenthesized expression should be parenthesized when the expression has a bunch of the same kind of operator.

Order of evaluation rules describe the order in which each operand in an expression is evaluated.

Higher precedence results in grouping of operands with an operator and doesn't mean the evaluation of operands. It is the order of evaluation which decides sequence of evaluation of sub-expressions in an expression.


Update:

As I can see many programmers think that statement

a = ++a * ( ++a + 5);   

will invoke undefined behavior. Yes it will invoke UB if there is no guarantee of evaluation order of operands of an operator.

But this is not true in context of java programming language. It has well defined behavior in java (as well as in C#). The Java Language Specification states that:

15.7. Evaluation Order

The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.

Example 15.7.1-1. Left-Hand Operand Is Evaluated First

In the following program, the * operator has a left-hand operand that contains an assignment to a variable and a right-hand operand that contains a reference to the same variable. The value produced by the reference will reflect the fact that the assignment occurred first.

class Test1 {     public static void main(String[] args) {         int i = 2;         int j = (i=3) * i;         System.out.println(j);     } } 

This program produces the output:

9 

It is not permitted for evaluation of the * operator to produce 6 instead of 9.

But, still java specification clearly states that not to write such codes:

It is recommended that code not rely crucially on this specification. Code is usually clearer when each expression contains at most one side effect, as its outermost operation, and when code does not depend on exactly which exception arises as a consequence of the left-to-right evaluation of expressions.

like image 34
haccks Avatar answered Sep 19 '22 20:09

haccks