4
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by kommuru
Featured Replies
  • 2
    Aia 1,977   7 Years Ago

    [QUOTE=jk2005.jeeva;1048400] [COLOR="Green"]int[/COLOR] main([COLOR="Green"]void[/COLOR]) { int x=20,y=35; [COLOR="Red"] x=y++ + x++; y= ++y + ++x; [/COLOR] printf(“%d%d[COLOR="Red"]\[/COLOR]n”,x,y); /* is should be \n */ [COLOR="Green"]return 0;[/COLOR] } [/QUOTE] Those two expressions invoke undefined behavior. Anything can be the result. It is best if you write [COLOR="Red"]int[/COLOR] main(), and [COLOR="Red"]return[/COLOR] inside of main … Read More

  • 1
    Aia 1,977   7 Years Ago

    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 … Read More

1

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?

Edited by Nick Evan: Add code-tags

2

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.

Edited by Aia: n/a

-1

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.

Votes + Comments
undefined behavior means what works on one compiler may not work on another compiler.
1

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.

Edited by mike_2000_17: Fixed formatting

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.