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

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.

commented: Please revisit http://www.daniweb.com/forums/thread83018.html -2
commented: Nope. -2

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.

commented: Nothing but gcc. +6

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.

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.

hi,
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;
i=j++*++j*j--;

for this it takes first two variables and does the operation

j++*++j

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.

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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.