What is the result of i == (i = 2)?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
38
down vote

favorite
6












Run the following code:



// In Java, output #####
public static void main(String args)
int i = 1;

if(i == (i = 2))
System.out.println("@@@@@");
else
System.out.println("#####");




But:



// In C, output @@@@@,I did test on Clion(GCC 7.3) and Visual Studio 2017
int main(int argc, char *argv)
int i = 1;

if(i == (i = 2))
printf("@@@@@");
else
printf("#####");


return 0;



The motivation for asking this question comes from the following code:



// The code is from the JDK 11 - java.util.concurrent.atomic.AtomicInteger
// I am curious about the behavior of the variable prev.
public final int getAndUpdate(IntUnaryOperator updateFunction)
int prev = get(), next = 0;
for (boolean haveNext = false;;)
if (!haveNext)
next = updateFunction.applyAsInt(prev);
if (weakCompareAndSetVolatile(prev, next))
return prev;
haveNext = (prev == (prev = get()));




So, how to explain the above two different execution modes?










share|improve this question



















  • 12




    One explains the two different execution modes by first noting that those are two entirely different languages. They happen to share some syntax, but that's where the similarities end.
    – StoryTeller
    Dec 2 at 6:44






  • 3




    The result is : a messy code. Better not imitate this unless you are running for a java obfuscation contest.
    – Tristan
    Dec 2 at 7:29






  • 4




    Possible duplicate of Logic differences in C and Java
    – user202729
    Dec 2 at 8:16






  • 1




    Undefined behavior and sequence points
    – phuclv
    Dec 2 at 16:32






  • 1




    @TheGreatDuck After your edit, the question is still valid and is no longer a duplicate, but the top voted answer contains a lot of unrelated parts (and the "two execution mode" statement no longer makes sense).
    – user202729
    Dec 4 at 10:06














up vote
38
down vote

favorite
6












Run the following code:



// In Java, output #####
public static void main(String args)
int i = 1;

if(i == (i = 2))
System.out.println("@@@@@");
else
System.out.println("#####");




But:



// In C, output @@@@@,I did test on Clion(GCC 7.3) and Visual Studio 2017
int main(int argc, char *argv)
int i = 1;

if(i == (i = 2))
printf("@@@@@");
else
printf("#####");


return 0;



The motivation for asking this question comes from the following code:



// The code is from the JDK 11 - java.util.concurrent.atomic.AtomicInteger
// I am curious about the behavior of the variable prev.
public final int getAndUpdate(IntUnaryOperator updateFunction)
int prev = get(), next = 0;
for (boolean haveNext = false;;)
if (!haveNext)
next = updateFunction.applyAsInt(prev);
if (weakCompareAndSetVolatile(prev, next))
return prev;
haveNext = (prev == (prev = get()));




So, how to explain the above two different execution modes?










share|improve this question



















  • 12




    One explains the two different execution modes by first noting that those are two entirely different languages. They happen to share some syntax, but that's where the similarities end.
    – StoryTeller
    Dec 2 at 6:44






  • 3




    The result is : a messy code. Better not imitate this unless you are running for a java obfuscation contest.
    – Tristan
    Dec 2 at 7:29






  • 4




    Possible duplicate of Logic differences in C and Java
    – user202729
    Dec 2 at 8:16






  • 1




    Undefined behavior and sequence points
    – phuclv
    Dec 2 at 16:32






  • 1




    @TheGreatDuck After your edit, the question is still valid and is no longer a duplicate, but the top voted answer contains a lot of unrelated parts (and the "two execution mode" statement no longer makes sense).
    – user202729
    Dec 4 at 10:06












up vote
38
down vote

favorite
6









up vote
38
down vote

favorite
6






6





Run the following code:



// In Java, output #####
public static void main(String args)
int i = 1;

if(i == (i = 2))
System.out.println("@@@@@");
else
System.out.println("#####");




But:



// In C, output @@@@@,I did test on Clion(GCC 7.3) and Visual Studio 2017
int main(int argc, char *argv)
int i = 1;

if(i == (i = 2))
printf("@@@@@");
else
printf("#####");


return 0;



The motivation for asking this question comes from the following code:



// The code is from the JDK 11 - java.util.concurrent.atomic.AtomicInteger
// I am curious about the behavior of the variable prev.
public final int getAndUpdate(IntUnaryOperator updateFunction)
int prev = get(), next = 0;
for (boolean haveNext = false;;)
if (!haveNext)
next = updateFunction.applyAsInt(prev);
if (weakCompareAndSetVolatile(prev, next))
return prev;
haveNext = (prev == (prev = get()));




So, how to explain the above two different execution modes?










share|improve this question















Run the following code:



// In Java, output #####
public static void main(String args)
int i = 1;

if(i == (i = 2))
System.out.println("@@@@@");
else
System.out.println("#####");




But:



// In C, output @@@@@,I did test on Clion(GCC 7.3) and Visual Studio 2017
int main(int argc, char *argv)
int i = 1;

if(i == (i = 2))
printf("@@@@@");
else
printf("#####");


return 0;



The motivation for asking this question comes from the following code:



// The code is from the JDK 11 - java.util.concurrent.atomic.AtomicInteger
// I am curious about the behavior of the variable prev.
public final int getAndUpdate(IntUnaryOperator updateFunction)
int prev = get(), next = 0;
for (boolean haveNext = false;;)
if (!haveNext)
next = updateFunction.applyAsInt(prev);
if (weakCompareAndSetVolatile(prev, next))
return prev;
haveNext = (prev == (prev = get()));




So, how to explain the above two different execution modes?







java c language-lawyer variable-assignment side-effects






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 4 at 10:15









Sneftel

23.5k64077




23.5k64077










asked Dec 2 at 5:41









kangjianwei

431510




431510







  • 12




    One explains the two different execution modes by first noting that those are two entirely different languages. They happen to share some syntax, but that's where the similarities end.
    – StoryTeller
    Dec 2 at 6:44






  • 3




    The result is : a messy code. Better not imitate this unless you are running for a java obfuscation contest.
    – Tristan
    Dec 2 at 7:29






  • 4




    Possible duplicate of Logic differences in C and Java
    – user202729
    Dec 2 at 8:16






  • 1




    Undefined behavior and sequence points
    – phuclv
    Dec 2 at 16:32






  • 1




    @TheGreatDuck After your edit, the question is still valid and is no longer a duplicate, but the top voted answer contains a lot of unrelated parts (and the "two execution mode" statement no longer makes sense).
    – user202729
    Dec 4 at 10:06












  • 12




    One explains the two different execution modes by first noting that those are two entirely different languages. They happen to share some syntax, but that's where the similarities end.
    – StoryTeller
    Dec 2 at 6:44






  • 3




    The result is : a messy code. Better not imitate this unless you are running for a java obfuscation contest.
    – Tristan
    Dec 2 at 7:29






  • 4




    Possible duplicate of Logic differences in C and Java
    – user202729
    Dec 2 at 8:16






  • 1




    Undefined behavior and sequence points
    – phuclv
    Dec 2 at 16:32






  • 1




    @TheGreatDuck After your edit, the question is still valid and is no longer a duplicate, but the top voted answer contains a lot of unrelated parts (and the "two execution mode" statement no longer makes sense).
    – user202729
    Dec 4 at 10:06







12




12




One explains the two different execution modes by first noting that those are two entirely different languages. They happen to share some syntax, but that's where the similarities end.
– StoryTeller
Dec 2 at 6:44




One explains the two different execution modes by first noting that those are two entirely different languages. They happen to share some syntax, but that's where the similarities end.
– StoryTeller
Dec 2 at 6:44




3




3




The result is : a messy code. Better not imitate this unless you are running for a java obfuscation contest.
– Tristan
Dec 2 at 7:29




The result is : a messy code. Better not imitate this unless you are running for a java obfuscation contest.
– Tristan
Dec 2 at 7:29




4




4




Possible duplicate of Logic differences in C and Java
– user202729
Dec 2 at 8:16




Possible duplicate of Logic differences in C and Java
– user202729
Dec 2 at 8:16




1




1




Undefined behavior and sequence points
– phuclv
Dec 2 at 16:32




Undefined behavior and sequence points
– phuclv
Dec 2 at 16:32




1




1




@TheGreatDuck After your edit, the question is still valid and is no longer a duplicate, but the top voted answer contains a lot of unrelated parts (and the "two execution mode" statement no longer makes sense).
– user202729
Dec 4 at 10:06




@TheGreatDuck After your edit, the question is still valid and is no longer a duplicate, but the top voted answer contains a lot of unrelated parts (and the "two execution mode" statement no longer makes sense).
– user202729
Dec 4 at 10:06












3 Answers
3






active

oldest

votes

















up vote
56
down vote













The behaviour of a C program that executes the expression i == (i = 2) is undefined.



It comes from C11 6.5p22:




  1. If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.84)



The i on the left-hand side of == is a value computation on the value of scalar object i and the right-hand side i = 2 has a side effect of assigning the value 2 to i. The LHS and RHS of == are unsequenced w.r.t. each other. Hence the entire program is meaningless in C.



Compile with gcc -Wall and GCC will spit out:



unsequenced.c:5:16: warning: operation on ‘i’ may be undefined [-Wsequence-point]
if(i == (i = 2)) {
~~~^~~~



Unlike C, Java guarantees the evaluation order for operands (left-to-right), therefore



haveNext = (prev == (prev = get()));


is correct in Java. The value of LHS is determined strictly before the evaluation of the side effect on the RHS occurs.



In C you have to write this as something like



newPrev = get();
haveNext = (prev == newPrev);
prev = newPrev;





share|improve this answer






















  • Why is i on the LHS a value computation? Is it specified somewhere in the Standard?
    – Some Name
    Dec 2 at 6:11






  • 1




    @SomeName of course it is specified somewhere in the standard. :D
    – Antti Haapala
    Dec 2 at 6:11










  • So how about reference? :) The only I could find was the value term defined in 3.19, but no value computation defined there.
    – Some Name
    Dec 2 at 6:13










  • @SomeName you need to infer it from way too many places in the prose :/ the footnote 84 however can be used as a shortcut. If i there was not a value of an expression then a[i++] = i; would be defined. Footnotes are not normative though.
    – Antti Haapala
    Dec 2 at 6:34







  • 1




    @SomeName 5.1.2.3p2 says "value computation of lvalue expression", which is a case here, i is an lvalue and its value need to be computed, as it is not an operand. 6.3.2.1p2 says "converted to the value stored in the designated object", but this is the one referred to by 5.1.2.3p2
    – Antti Haapala
    Dec 2 at 6:39

















up vote
15
down vote













The Java Language Specification (§15.7) states:




The Java programming language guarantees that the operands of operators appear
to be evaluated in a specific evaluation order, namely, from left to right.




The specification (§15.21.1) also states that:




The value produced by the == operator is true if the value of the left-hand
operand is equal to the value of the right-hand operand; otherwise, the result is
false.




Therefore in Java, the if-statement at runtime would look like the following, which obviously evaluates to false:



if (1 == 2) 




In C, it is simply undefined (see Antti's answer).






share|improve this answer





























    up vote
    5
    down vote













    In C, the behavior of i == (i = 2) is undefined because it attempts to both update an object and use that object’s value in a computation without an intervening sequence point. The result will vary based on the compiler, compiler settings, even the surrounding code.






    share|improve this answer




















    • what about Java?
      – The Great Duck
      Dec 3 at 3:57










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53577739%2fwhat-is-the-result-of-i-i-2%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    56
    down vote













    The behaviour of a C program that executes the expression i == (i = 2) is undefined.



    It comes from C11 6.5p22:




    1. If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.84)



    The i on the left-hand side of == is a value computation on the value of scalar object i and the right-hand side i = 2 has a side effect of assigning the value 2 to i. The LHS and RHS of == are unsequenced w.r.t. each other. Hence the entire program is meaningless in C.



    Compile with gcc -Wall and GCC will spit out:



    unsequenced.c:5:16: warning: operation on ‘i’ may be undefined [-Wsequence-point]
    if(i == (i = 2)) {
    ~~~^~~~



    Unlike C, Java guarantees the evaluation order for operands (left-to-right), therefore



    haveNext = (prev == (prev = get()));


    is correct in Java. The value of LHS is determined strictly before the evaluation of the side effect on the RHS occurs.



    In C you have to write this as something like



    newPrev = get();
    haveNext = (prev == newPrev);
    prev = newPrev;





    share|improve this answer






















    • Why is i on the LHS a value computation? Is it specified somewhere in the Standard?
      – Some Name
      Dec 2 at 6:11






    • 1




      @SomeName of course it is specified somewhere in the standard. :D
      – Antti Haapala
      Dec 2 at 6:11










    • So how about reference? :) The only I could find was the value term defined in 3.19, but no value computation defined there.
      – Some Name
      Dec 2 at 6:13










    • @SomeName you need to infer it from way too many places in the prose :/ the footnote 84 however can be used as a shortcut. If i there was not a value of an expression then a[i++] = i; would be defined. Footnotes are not normative though.
      – Antti Haapala
      Dec 2 at 6:34







    • 1




      @SomeName 5.1.2.3p2 says "value computation of lvalue expression", which is a case here, i is an lvalue and its value need to be computed, as it is not an operand. 6.3.2.1p2 says "converted to the value stored in the designated object", but this is the one referred to by 5.1.2.3p2
      – Antti Haapala
      Dec 2 at 6:39














    up vote
    56
    down vote













    The behaviour of a C program that executes the expression i == (i = 2) is undefined.



    It comes from C11 6.5p22:




    1. If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.84)



    The i on the left-hand side of == is a value computation on the value of scalar object i and the right-hand side i = 2 has a side effect of assigning the value 2 to i. The LHS and RHS of == are unsequenced w.r.t. each other. Hence the entire program is meaningless in C.



    Compile with gcc -Wall and GCC will spit out:



    unsequenced.c:5:16: warning: operation on ‘i’ may be undefined [-Wsequence-point]
    if(i == (i = 2)) {
    ~~~^~~~



    Unlike C, Java guarantees the evaluation order for operands (left-to-right), therefore



    haveNext = (prev == (prev = get()));


    is correct in Java. The value of LHS is determined strictly before the evaluation of the side effect on the RHS occurs.



    In C you have to write this as something like



    newPrev = get();
    haveNext = (prev == newPrev);
    prev = newPrev;





    share|improve this answer






















    • Why is i on the LHS a value computation? Is it specified somewhere in the Standard?
      – Some Name
      Dec 2 at 6:11






    • 1




      @SomeName of course it is specified somewhere in the standard. :D
      – Antti Haapala
      Dec 2 at 6:11










    • So how about reference? :) The only I could find was the value term defined in 3.19, but no value computation defined there.
      – Some Name
      Dec 2 at 6:13










    • @SomeName you need to infer it from way too many places in the prose :/ the footnote 84 however can be used as a shortcut. If i there was not a value of an expression then a[i++] = i; would be defined. Footnotes are not normative though.
      – Antti Haapala
      Dec 2 at 6:34







    • 1




      @SomeName 5.1.2.3p2 says "value computation of lvalue expression", which is a case here, i is an lvalue and its value need to be computed, as it is not an operand. 6.3.2.1p2 says "converted to the value stored in the designated object", but this is the one referred to by 5.1.2.3p2
      – Antti Haapala
      Dec 2 at 6:39












    up vote
    56
    down vote










    up vote
    56
    down vote









    The behaviour of a C program that executes the expression i == (i = 2) is undefined.



    It comes from C11 6.5p22:




    1. If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.84)



    The i on the left-hand side of == is a value computation on the value of scalar object i and the right-hand side i = 2 has a side effect of assigning the value 2 to i. The LHS and RHS of == are unsequenced w.r.t. each other. Hence the entire program is meaningless in C.



    Compile with gcc -Wall and GCC will spit out:



    unsequenced.c:5:16: warning: operation on ‘i’ may be undefined [-Wsequence-point]
    if(i == (i = 2)) {
    ~~~^~~~



    Unlike C, Java guarantees the evaluation order for operands (left-to-right), therefore



    haveNext = (prev == (prev = get()));


    is correct in Java. The value of LHS is determined strictly before the evaluation of the side effect on the RHS occurs.



    In C you have to write this as something like



    newPrev = get();
    haveNext = (prev == newPrev);
    prev = newPrev;





    share|improve this answer














    The behaviour of a C program that executes the expression i == (i = 2) is undefined.



    It comes from C11 6.5p22:




    1. If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.84)



    The i on the left-hand side of == is a value computation on the value of scalar object i and the right-hand side i = 2 has a side effect of assigning the value 2 to i. The LHS and RHS of == are unsequenced w.r.t. each other. Hence the entire program is meaningless in C.



    Compile with gcc -Wall and GCC will spit out:



    unsequenced.c:5:16: warning: operation on ‘i’ may be undefined [-Wsequence-point]
    if(i == (i = 2)) {
    ~~~^~~~



    Unlike C, Java guarantees the evaluation order for operands (left-to-right), therefore



    haveNext = (prev == (prev = get()));


    is correct in Java. The value of LHS is determined strictly before the evaluation of the side effect on the RHS occurs.



    In C you have to write this as something like



    newPrev = get();
    haveNext = (prev == newPrev);
    prev = newPrev;






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 2 at 6:40

























    answered Dec 2 at 5:50









    Antti Haapala

    79.4k16148192




    79.4k16148192











    • Why is i on the LHS a value computation? Is it specified somewhere in the Standard?
      – Some Name
      Dec 2 at 6:11






    • 1




      @SomeName of course it is specified somewhere in the standard. :D
      – Antti Haapala
      Dec 2 at 6:11










    • So how about reference? :) The only I could find was the value term defined in 3.19, but no value computation defined there.
      – Some Name
      Dec 2 at 6:13










    • @SomeName you need to infer it from way too many places in the prose :/ the footnote 84 however can be used as a shortcut. If i there was not a value of an expression then a[i++] = i; would be defined. Footnotes are not normative though.
      – Antti Haapala
      Dec 2 at 6:34







    • 1




      @SomeName 5.1.2.3p2 says "value computation of lvalue expression", which is a case here, i is an lvalue and its value need to be computed, as it is not an operand. 6.3.2.1p2 says "converted to the value stored in the designated object", but this is the one referred to by 5.1.2.3p2
      – Antti Haapala
      Dec 2 at 6:39
















    • Why is i on the LHS a value computation? Is it specified somewhere in the Standard?
      – Some Name
      Dec 2 at 6:11






    • 1




      @SomeName of course it is specified somewhere in the standard. :D
      – Antti Haapala
      Dec 2 at 6:11










    • So how about reference? :) The only I could find was the value term defined in 3.19, but no value computation defined there.
      – Some Name
      Dec 2 at 6:13










    • @SomeName you need to infer it from way too many places in the prose :/ the footnote 84 however can be used as a shortcut. If i there was not a value of an expression then a[i++] = i; would be defined. Footnotes are not normative though.
      – Antti Haapala
      Dec 2 at 6:34







    • 1




      @SomeName 5.1.2.3p2 says "value computation of lvalue expression", which is a case here, i is an lvalue and its value need to be computed, as it is not an operand. 6.3.2.1p2 says "converted to the value stored in the designated object", but this is the one referred to by 5.1.2.3p2
      – Antti Haapala
      Dec 2 at 6:39















    Why is i on the LHS a value computation? Is it specified somewhere in the Standard?
    – Some Name
    Dec 2 at 6:11




    Why is i on the LHS a value computation? Is it specified somewhere in the Standard?
    – Some Name
    Dec 2 at 6:11




    1




    1




    @SomeName of course it is specified somewhere in the standard. :D
    – Antti Haapala
    Dec 2 at 6:11




    @SomeName of course it is specified somewhere in the standard. :D
    – Antti Haapala
    Dec 2 at 6:11












    So how about reference? :) The only I could find was the value term defined in 3.19, but no value computation defined there.
    – Some Name
    Dec 2 at 6:13




    So how about reference? :) The only I could find was the value term defined in 3.19, but no value computation defined there.
    – Some Name
    Dec 2 at 6:13












    @SomeName you need to infer it from way too many places in the prose :/ the footnote 84 however can be used as a shortcut. If i there was not a value of an expression then a[i++] = i; would be defined. Footnotes are not normative though.
    – Antti Haapala
    Dec 2 at 6:34





    @SomeName you need to infer it from way too many places in the prose :/ the footnote 84 however can be used as a shortcut. If i there was not a value of an expression then a[i++] = i; would be defined. Footnotes are not normative though.
    – Antti Haapala
    Dec 2 at 6:34





    1




    1




    @SomeName 5.1.2.3p2 says "value computation of lvalue expression", which is a case here, i is an lvalue and its value need to be computed, as it is not an operand. 6.3.2.1p2 says "converted to the value stored in the designated object", but this is the one referred to by 5.1.2.3p2
    – Antti Haapala
    Dec 2 at 6:39




    @SomeName 5.1.2.3p2 says "value computation of lvalue expression", which is a case here, i is an lvalue and its value need to be computed, as it is not an operand. 6.3.2.1p2 says "converted to the value stored in the designated object", but this is the one referred to by 5.1.2.3p2
    – Antti Haapala
    Dec 2 at 6:39












    up vote
    15
    down vote













    The Java Language Specification (§15.7) states:




    The Java programming language guarantees that the operands of operators appear
    to be evaluated in a specific evaluation order, namely, from left to right.




    The specification (§15.21.1) also states that:




    The value produced by the == operator is true if the value of the left-hand
    operand is equal to the value of the right-hand operand; otherwise, the result is
    false.




    Therefore in Java, the if-statement at runtime would look like the following, which obviously evaluates to false:



    if (1 == 2) 




    In C, it is simply undefined (see Antti's answer).






    share|improve this answer


























      up vote
      15
      down vote













      The Java Language Specification (§15.7) states:




      The Java programming language guarantees that the operands of operators appear
      to be evaluated in a specific evaluation order, namely, from left to right.




      The specification (§15.21.1) also states that:




      The value produced by the == operator is true if the value of the left-hand
      operand is equal to the value of the right-hand operand; otherwise, the result is
      false.




      Therefore in Java, the if-statement at runtime would look like the following, which obviously evaluates to false:



      if (1 == 2) 




      In C, it is simply undefined (see Antti's answer).






      share|improve this answer
























        up vote
        15
        down vote










        up vote
        15
        down vote









        The Java Language Specification (§15.7) states:




        The Java programming language guarantees that the operands of operators appear
        to be evaluated in a specific evaluation order, namely, from left to right.




        The specification (§15.21.1) also states that:




        The value produced by the == operator is true if the value of the left-hand
        operand is equal to the value of the right-hand operand; otherwise, the result is
        false.




        Therefore in Java, the if-statement at runtime would look like the following, which obviously evaluates to false:



        if (1 == 2) 




        In C, it is simply undefined (see Antti's answer).






        share|improve this answer














        The Java Language Specification (§15.7) states:




        The Java programming language guarantees that the operands of operators appear
        to be evaluated in a specific evaluation order, namely, from left to right.




        The specification (§15.21.1) also states that:




        The value produced by the == operator is true if the value of the left-hand
        operand is equal to the value of the right-hand operand; otherwise, the result is
        false.




        Therefore in Java, the if-statement at runtime would look like the following, which obviously evaluates to false:



        if (1 == 2) 




        In C, it is simply undefined (see Antti's answer).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 2 at 6:04

























        answered Dec 2 at 5:57









        Jacob G.

        15k51962




        15k51962




















            up vote
            5
            down vote













            In C, the behavior of i == (i = 2) is undefined because it attempts to both update an object and use that object’s value in a computation without an intervening sequence point. The result will vary based on the compiler, compiler settings, even the surrounding code.






            share|improve this answer




















            • what about Java?
              – The Great Duck
              Dec 3 at 3:57














            up vote
            5
            down vote













            In C, the behavior of i == (i = 2) is undefined because it attempts to both update an object and use that object’s value in a computation without an intervening sequence point. The result will vary based on the compiler, compiler settings, even the surrounding code.






            share|improve this answer




















            • what about Java?
              – The Great Duck
              Dec 3 at 3:57












            up vote
            5
            down vote










            up vote
            5
            down vote









            In C, the behavior of i == (i = 2) is undefined because it attempts to both update an object and use that object’s value in a computation without an intervening sequence point. The result will vary based on the compiler, compiler settings, even the surrounding code.






            share|improve this answer












            In C, the behavior of i == (i = 2) is undefined because it attempts to both update an object and use that object’s value in a computation without an intervening sequence point. The result will vary based on the compiler, compiler settings, even the surrounding code.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 2 at 5:55









            John Bode

            80.8k1375149




            80.8k1375149











            • what about Java?
              – The Great Duck
              Dec 3 at 3:57
















            • what about Java?
              – The Great Duck
              Dec 3 at 3:57















            what about Java?
            – The Great Duck
            Dec 3 at 3:57




            what about Java?
            – The Great Duck
            Dec 3 at 3:57

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53577739%2fwhat-is-the-result-of-i-i-2%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?