what is the result of i and how it is evaluated please help me.
the result is 125 and i don't understand the evaluation part
First an increment occurs in i = j++ * ++j * j--;
Now j = 4+1 = 5.
Second: expression i = j++ * ++j * j--; is evaluated.
Meaning i = ( j * j ) * j = 5 * 5 * 5 = 125; and there's your value showed by printf().
After that another increment occurrs followed by a decrement.
i = j++ * ++j * j--;
Meaning j = 5 + 1 - 1 = 5.
Even when the associativity of uniry operators works from right to left, ++ is higher than -- in their order of precedence, so ++ gets evaluated first.
If you do a printf( "%d\n", j ); after all the expressions are evaluated you should get 5.
Indeed it seems that the only thing which standard says concerning the unary ++ and -- operators, is that the value of the operand would be obtained for use in expression, either before (postfix) or after (prefix) applying the operator. What remains unspecified is the order of evaluation of the operands (except precedence) and when the postfix operator would be applied.
Using the gcc compiler, the value was indeed 125, which means that gcc compiler does it the most determined way, ie applies the postfix operators only after evaluating the whole expression.
But using tcc compiler, the result was 144, which means that the postfix operators were applied immediately after obtaining the value of their operand.
Therefore using ++ or -- operators in an expression when their operand would be used later in that same expression, is something which can cause unspecified behaviour, and should therefore be avoided. What is unspecified is when the operator would be applied after obtaining the value of the operand.
I think I have a couple questions if you don't mind.
Is there any authoritative book or work that describe all the undefined behaviors in C?.
Or are you suppose to guess when you read the Standards that whatever is not written there is undefined behaviour?
Since we are taking about the Standards, where can I get these Standards?. Where can I find a trusted source that explains the Standards?.
Sorry, if it sounds like too many questions.
It seems to me that there's more undefined behaviours in C that actually specified behaviours, and I would like to learn about them, instead of getting hammer down when I try to put in practice something I have read in some C programming books.
2 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 accessed only to determine the value to be stored.60)
Where the footnote 60 is
60) This paragraph renders undefined statement expressions such as
i = ++i + 1;
a[i++] = i;
i =i +1;
a[i] = i;
As soon as you have multiple side effects on the same variable, or multiple references to a variable which has side effects, you're basically in UB territory.