Hi
Anyonem, could you please explain clearly about the order of evaluation of increment, decrement operators in an expression in C?
Thanks in advance..
Hi
Anyonem, could you please explain clearly about the order of evaluation of increment, decrement operators in an expression in C?
Thanks in advance..
Its really a very simple concept (link here)
Thanks, But can you please explain it with this example?
main()
{
int x=20,y=35;
x=y++ + x++;
y= ++y + ++x;
printf(â%d%dnâ,x,y);
}
By solving this, i got 5592, 55 for first x by adding both x and y before increment them ,since they are post increment operators, then in next expression both x and y get incremented and added to give y value as 92. Right? But the computer shows the answer as 5794. How is it possible?
int main(void)
{
int x=20,y=35;
x=y++ + x++;
y= ++y + ++x;
printf(â%d%d\nâ,x,y); /* is should be \n */
return 0;
}
Those two expressions invoke undefined behavior. Anything can be the result.
It is best if you write int main(), and return inside of main to comply with the standard.
According to your result, the exeuction has been done as follows:
just check the values of x and y after each statement
int x=20,y=35; x = 20 , y= 35
x=y++ + x++; post increment so no change in x and y
x= 20+35 = 55, y=35
After the execution of the above statement, the values in x and y get incremented, so x = 56 and y = 36
y= ++y + ++x; Here preincrement for both x and y so the
x becomes 57 and y becomes 37
y= 57 + 37 = 94
when you print x and y , 57 and 94 are displayed. I hope its clear now.
Expressions has direct effects and side effects.
This expression, x = y;
has a direct effect of copying the computed value of y
assigning it to the location x
.
x = a[i];
has a direct effect as well.x = a[i++];
has a direct effect and a side effect. After the value of a[i]
gets copied to x
, one is added to the value of i
. This works fine because of the sequence points. After the statement has completed execution, both x
and i
has its value changed in a predictable way.
x = a[i++] + b[i++];
in the other hand is unpredictable. The precedence of the operators are defined by the C Standards but the order of execution is not. It is possible that a[i] + b[i]
gets added first and then assign to x
, or it is possible that i++
gets increment first and then the sum of a[i] + b[i]
gets assign to x
.
Heck, it is even possible (anything is possible) that in a bizarre way i
gets incremented for the a
subscript and then for the b
subscript before the sum of both gets assign to x
The rule of thumb is not to rely on the order of precedence between sequence points. A sequence point is the guarantee that all direct and side effects are completed at that point. Sequence points are:
; (semicolon)
, (coma)
|| (OR)
&& (AND)
?: ( question mark and colon)
and any expression as a parameter for a function call.
Now, if you look back to the previously posted x=y++ + x++;
we can see that it is not possible to know in a predictable way the final value of x, taking in consideration what I just explained. However, there's more. [1]There's a rule in the standard that says that between sequence points a object can have its value changed only once, and that the previous value shall be access only for determining the value stored.
This statement x = x++;
would break that rule.
[1] In my own words.
Aia, Thanks for the good information