Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do parentheses force order of evaluation and make an undefined expression defined?

I was just going though my text book when I came across this question

  1. What would be the value of a after the following expression ?
    Assume the initial value of a = 5.Mention the steps
    • a+=(a++)+(++a)

At first I thought this is undefined behaviour because a has been modified more than once.
So then I read the question and it said Mention the steps so I probably thought this question is right.

So my question is :

  • Does applying parentheses make an undefined behaviour defined ?
  • Is a sequence point created after evaluating a parentheses expression ?
  • If it is defined,how does the parentheses matter since ++ and () have the same precedence

Note: A well explained and clear answer will get my vote

like image 669
DollarAkshay Avatar asked Apr 02 '14 16:04

DollarAkshay


People also ask

What determines the order in which expression are evaluated?

Precedence, in a conceptual sense, determines which one out of two operators is evaluated "first". Another way to put it is, precedence determines how tightly an operator binds to its operands as compared to the other applicable operator in an expression.

What is an expression explain order of evaluation?

An expression can contain several operators with equal precedence. When several such operators appear at the same level in an expression, evaluation proceeds according to the associativity of the operator, either from right to left or from left to right.

What is the typical evaluation order for an assignment expression?

The Usual Order ..., when evaluating the operands of an expression, assignment, or return statement, all function calls, method calls, and (channel) communication operations are evaluated in lexical left-to-right order.

How parentheses can be used to change the precedence?

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

No, applying parentheses doesn't make it a defined behaviour. It's still undefined. The C99 standard §6.5 ¶2 says

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.

Putting a sub-expression in parentheses may force the order of evaluation of sub-expressions but it does not create a sequence point. Therefore, it does not guarantee when the side effects of the sub-expressions, if they produce any, will take place. Quoting the C99 standard again §5.1.2.3¶2

Evaluation of an expression may produce side effects. At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place.

For the sake of completeness, following are sequence points laid down by the C99 standard in Annex C.

  1. The call to a function, after the arguments have been evaluated.

  2. The end of the first operand of the following operators: logical AND &&; logical OR ||; conditional ?; comma ,.

  3. The end of a full declarator.

  4. The end of a full expression; the expression in an expression statement; the controlling expression of a selection statement (if or switch); the controlling expression of a while or do statement; each of the expressions of a for statement; the expression in a return statement.

  5. Immediately before a library function returns.

  6. After the actions associated with each formatted input/output function conversion specifier.

  7. Immediately before and immediately after each call to a comparison function, and also between any call to a comparison function and any movement of the objects passed as arguments to that call.

like image 189
ajay Avatar answered Oct 12 '22 23:10

ajay