954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

please helpme i have small doubt in post and pre increment operators

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

satish_dukkipat
Newbie Poster
3 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 
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
 

The above is undefined behavior in C?C++
https://www.securecoding.cert.org/confluence/display/cplusplus/EXP34-C.+Do+not+depend+on+order+of+evaluation+between+sequence+points
This code in Java & C# is defined, I would be:

int i,j=4;
i=j++*++j*j--;


i = 4(5) *(6)6 * 6(5) = 144- Which is what I got when running the program in Java...

SirJames
Newbie Poster
5 posts since Jul 2007
Reputation Points: 20
Solved Threads: 0
 

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.

TkTkorrovi
Junior Poster
170 posts since Mar 2005
Reputation Points: 85
Solved Threads: 13
 

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
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Please see http://www.iso-9899.info/wiki/Web_resources including the link to a draft standard. Yes everything which is not written in the standard must be considered unspecified.

TkTkorrovi
Junior Poster
170 posts since Mar 2005
Reputation Points: 85
Solved Threads: 13
 

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.

vvidyadhara
Newbie Poster
3 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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

TkTkorrovi
Junior Poster
170 posts since Mar 2005
Reputation Points: 85
Solved Threads: 13
 
Wrong, order of evaluation is not specified except precedence and operator rules.


ThanQ kTkorrovi

vvidyadhara
Newbie Poster
3 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

> 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
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You