What is the result of i == (i = 2)?
up vote
6
down vote
favorite
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
add a comment |
up vote
6
down vote
favorite
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
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
1 min ago
add a comment |
up vote
6
down vote
favorite
up vote
6
down vote
favorite
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
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
java c
asked 1 hour ago
kangjianwei
26728
26728
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
1 min ago
add a comment |
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
1 min ago
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
1 min ago
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
1 min ago
add a comment |
4 Answers
4
active
oldest
votes
up vote
6
down vote
The behaviour of a C program that executes the expression i == (i = 2)
is undefined.
It comes from C11 6.5p22:
- 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;
Why isi
on the LHS a value computation? Is it specified somewhere in the Standard?
– Some Name
35 mins ago
@SomeName of course it is specified somewhere in the standard. :D
– Antti Haapala
34 mins ago
So how about reference? :) The only I could find was thevalue
term defined in 3.19, but novalue computation
defined there.
– Some Name
32 mins ago
@SomeName you need to infer it from way too many places in the prose :/ the footnote 84 however can be used as a shortcut. Ifi
there was not a value of an expression thena[i++] = i;
would be defined. Footnotes are not normative though.
– Antti Haapala
12 mins ago
@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
6 mins ago
add a comment |
up vote
3
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 istrue
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).
add a comment |
up vote
1
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.
add a comment |
up vote
-1
down vote
if(i == (i = 2))
in java language , the result of following statement will be 1 and but in c Language the result will be 2 and else section will run. the difference is in java first the condition will be checked and after that i value will change but in c first the value will change and after that condition will be checked and it's because of executive grades in java language
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
The behaviour of a C program that executes the expression i == (i = 2)
is undefined.
It comes from C11 6.5p22:
- 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;
Why isi
on the LHS a value computation? Is it specified somewhere in the Standard?
– Some Name
35 mins ago
@SomeName of course it is specified somewhere in the standard. :D
– Antti Haapala
34 mins ago
So how about reference? :) The only I could find was thevalue
term defined in 3.19, but novalue computation
defined there.
– Some Name
32 mins ago
@SomeName you need to infer it from way too many places in the prose :/ the footnote 84 however can be used as a shortcut. Ifi
there was not a value of an expression thena[i++] = i;
would be defined. Footnotes are not normative though.
– Antti Haapala
12 mins ago
@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
6 mins ago
add a comment |
up vote
6
down vote
The behaviour of a C program that executes the expression i == (i = 2)
is undefined.
It comes from C11 6.5p22:
- 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;
Why isi
on the LHS a value computation? Is it specified somewhere in the Standard?
– Some Name
35 mins ago
@SomeName of course it is specified somewhere in the standard. :D
– Antti Haapala
34 mins ago
So how about reference? :) The only I could find was thevalue
term defined in 3.19, but novalue computation
defined there.
– Some Name
32 mins ago
@SomeName you need to infer it from way too many places in the prose :/ the footnote 84 however can be used as a shortcut. Ifi
there was not a value of an expression thena[i++] = i;
would be defined. Footnotes are not normative though.
– Antti Haapala
12 mins ago
@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
6 mins ago
add a comment |
up vote
6
down vote
up vote
6
down vote
The behaviour of a C program that executes the expression i == (i = 2)
is undefined.
It comes from C11 6.5p22:
- 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;
The behaviour of a C program that executes the expression i == (i = 2)
is undefined.
It comes from C11 6.5p22:
- 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;
edited 6 mins ago
answered 55 mins ago
Antti Haapala
78.8k16147189
78.8k16147189
Why isi
on the LHS a value computation? Is it specified somewhere in the Standard?
– Some Name
35 mins ago
@SomeName of course it is specified somewhere in the standard. :D
– Antti Haapala
34 mins ago
So how about reference? :) The only I could find was thevalue
term defined in 3.19, but novalue computation
defined there.
– Some Name
32 mins ago
@SomeName you need to infer it from way too many places in the prose :/ the footnote 84 however can be used as a shortcut. Ifi
there was not a value of an expression thena[i++] = i;
would be defined. Footnotes are not normative though.
– Antti Haapala
12 mins ago
@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
6 mins ago
add a comment |
Why isi
on the LHS a value computation? Is it specified somewhere in the Standard?
– Some Name
35 mins ago
@SomeName of course it is specified somewhere in the standard. :D
– Antti Haapala
34 mins ago
So how about reference? :) The only I could find was thevalue
term defined in 3.19, but novalue computation
defined there.
– Some Name
32 mins ago
@SomeName you need to infer it from way too many places in the prose :/ the footnote 84 however can be used as a shortcut. Ifi
there was not a value of an expression thena[i++] = i;
would be defined. Footnotes are not normative though.
– Antti Haapala
12 mins ago
@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
6 mins ago
Why is
i
on the LHS a value computation? Is it specified somewhere in the Standard?– Some Name
35 mins ago
Why is
i
on the LHS a value computation? Is it specified somewhere in the Standard?– Some Name
35 mins ago
@SomeName of course it is specified somewhere in the standard. :D
– Antti Haapala
34 mins ago
@SomeName of course it is specified somewhere in the standard. :D
– Antti Haapala
34 mins ago
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
32 mins ago
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
32 mins ago
@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
12 mins ago
@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
12 mins ago
@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
6 mins ago
@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
6 mins ago
add a comment |
up vote
3
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 istrue
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).
add a comment |
up vote
3
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 istrue
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).
add a comment |
up vote
3
down vote
up vote
3
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 istrue
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).
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 istrue
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).
edited 41 mins ago
answered 48 mins ago
Jacob G.
14.7k51961
14.7k51961
add a comment |
add a comment |
up vote
1
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.
add a comment |
up vote
1
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.
add a comment |
up vote
1
down vote
up vote
1
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.
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.
answered 50 mins ago
John Bode
80.6k1375149
80.6k1375149
add a comment |
add a comment |
up vote
-1
down vote
if(i == (i = 2))
in java language , the result of following statement will be 1 and but in c Language the result will be 2 and else section will run. the difference is in java first the condition will be checked and after that i value will change but in c first the value will change and after that condition will be checked and it's because of executive grades in java language
add a comment |
up vote
-1
down vote
if(i == (i = 2))
in java language , the result of following statement will be 1 and but in c Language the result will be 2 and else section will run. the difference is in java first the condition will be checked and after that i value will change but in c first the value will change and after that condition will be checked and it's because of executive grades in java language
add a comment |
up vote
-1
down vote
up vote
-1
down vote
if(i == (i = 2))
in java language , the result of following statement will be 1 and but in c Language the result will be 2 and else section will run. the difference is in java first the condition will be checked and after that i value will change but in c first the value will change and after that condition will be checked and it's because of executive grades in java language
if(i == (i = 2))
in java language , the result of following statement will be 1 and but in c Language the result will be 2 and else section will run. the difference is in java first the condition will be checked and after that i value will change but in c first the value will change and after that condition will be checked and it's because of executive grades in java language
answered 43 mins ago
Nima Mohammadi
744
744
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
1 min ago