Why do C and Java treat the following loop differently -


in c

#include <stdio.h>   int main(){     int x=100;     x=x++ + x++;     printf("x :  %d\n",x); // prints 202     return 0; } 

in java

class demo{    public static void main(string args[]){       int x=100;      x=x++ +  x++;       system.out.printf("x : %d",x); // prints 201     } } 

why these 2 languages prints different values?what happens 'x=x++ + x++;' line ?

java , c different languages, not constructs syntactically valid in both (there many, since java's syntax builds on c's) must have same meaning in both.

the construct

int x=100;  x=x++ +  x++; 

is example. in java, evaluation order of expression specified left-to-right, , side-effect of incrementing x in x++ must have taken place before next part of expression evaluated. hence above code - except temporary variables - equivalent to

int x = 100; int left = x++;   // sets left x's value (100) , increments x, x 101 int right = x++;  // sets right x's current value (101) , increments x, x 102 x = left + right; // 100 + 101 = 201 

in java.

in c, order of evaluation of subexpressions composing expression not specified, , further, side-effect of incrementing x in x++ can take place @ time after previous sequence point , next sequence point.

therefore undefined behaviour in c modify same object more once without intervening sequence point (old parlance), in 1999 version of c language standard, expressed (in paragraph 2 of section 6.5) as

between previous , next sequence point object shall have stored value modified @ once evaluation of expression. furthermore, prior value shall read determine value stored.70)

70) paragraph renders undefined statement expressions such as

   = ++i + 1;     a[i++] = i; while allowing     = + 1;     a[i] = i; 

the current (2011) version of standard exchanged "sequence point" terminology "sequenced before/after" , "unsequenced", paragraph 2 of section 6.5 reads

if side effect on scalar object unsequenced relative either different side effect on same scalar object or value computation using value of same scalar object, behavior undefined. if there multiple allowable orderings of subexpressions of expression, behavior undefined if such unsequenced side effect occurs in of orderings.84)

(footnote 84 previous footnote 70).

the 2 side effects of incrementing scalar object x in x = x++ + x++, , side effect of storing computed value of right hand side in scalar object x unsequenced, hence behaviour undefined.

the compiler may reject compile programme containing expression x++ + x++ or ignore undefined behaviour (or in between). if compiles programme successfully, every runtime behaviour of line

x = x++ + x++; 

is equally valid. standard imposes no restrictions on behaviour.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -