Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

will right hand side of an expression always evaluated first

Will right side always evaluated to ahead of left side? And then the result of right side will be passed on to left side. I am not talking about the exception such as A[i]=i++

I am talking about the normal cases:

A[i] = (j+32+43 & K); 
A[j] != (A[j] + A[k]); 

will the right part of all these expression evaluated first and then the result is compared to the left side? (Always)

like image 961
codejam.tk Avatar asked Dec 15 '22 19:12

codejam.tk


1 Answers

In general the order of evaluation of sub-expressions is unspecified, there are a few exceptions such as logical and, logical or, comma operator, etc...

Since you comment stated you are interested in the general rule:

any operstor @YuHao if there is any general rule

that would be covered by the draft C99 standard section 6.5 Expressions paragraph 3 which says (emphasis mine going forward):

The grouping of operators and operands is indicated by the syntax.74) Except as specified later (for the function-call (), &&, ||, ?:, and comma operators), the order of evaluation of subexpressions and the order in which side effects take place are both unspecified.

this is basically the same in the draft C11 standard expect C11 does not list the exceptions so quoting C99 is more convenient. Paragraph 3 in C11 says:

The grouping of operators and operands is indicated by the syntax.85) Except as specified later, side effects and value computations of subexpressions are unsequenced.86)

Specifically for assignment operators C99 says:

The order of evaluation of the operands is unspecified [...]

and C11 says:

[...] The evaluations of the operands are unsequenced.

like image 63
Shafik Yaghmour Avatar answered Feb 08 '23 23:02

Shafik Yaghmour