int i,j=4;
i=j++*++j*j--;
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.
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
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.
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
http://std.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/
This is the last public draft (which is $0) of the C99 standard.
The key phrase being
6.5 Expressions
...
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;
while allowing 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.
Also http://c-faq.com/expr/index.html
Perhaps you should start burning anything which indicates otherwise.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
> so u can predict it without any problem.
Please learn to read posts before spouting any more bad advice.
What "your opinion" of how things work is doesn't matter, and is irrelevant to the issue.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953