10 Years
Discussion Span
Last Post by deceptikon

int i,j=4;
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.

Votes + Comments
Please revisit http://www.daniweb.com/forums/thread83018.html

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.

Votes + Comments
Nothing but gcc.

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.


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.


I think c is going to give the answer as 125 just because it is going to do the first operation just after having two numbers so as u look at

int i,j=4;

for this it takes first two variables and does the operation


if u give --j in the third position u are going to have 100
and if u put ++j going to give 150;

and for further same as in other languages. so u can predict it without any problem.


Wrong, order of evaluation is not specified except precedence and operator rules.

ThanQ kTkorrovi


> 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.


jj++++j*j-- i got the answer 400 but how it is ?help me

The answer you get is irrelevant since the entire expression is undefined due to modifying j multiple times between sequence points.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.