Does the statement `int val = (++i > ++j) ? ++i : ++j;` invoke undefined behavior?2019 Community Moderator Electionsequence points in cWhy are these constructs using pre and post-increment undefined behavior?Undefined, unspecified and implementation-defined behaviorIn C is there any guarantee with code prior to undefined behavior?Undefined behavior and sequence pointsUndefined behavior and sequence points reloadedIs the behavior of i = post_increment_i() specified, unspecified, or undefined?Undefined behavior and sequence pointUndefined behavior with: c = (b=a+2) - (a=1) ;Does a[a[0]] = 1 produce undefined behavior?printf with consecutive function as argument, undefined behavior

Citation at the bottom for subfigures in beamer frame

Why do Australian milk farmers need to protest supermarkets' milk price?

How to make healing in an exploration game interesting

Possible Leak In Concrete

Brexit - No Deal Rejection

Life insurance that covers only simultaneous/dual deaths

Cultural lunch issues

Could the Saturn V actually have launched astronauts around Venus?

PTIJ: Who should pay for Uber rides: the child or the parent?

Did CPM support custom hardware using device drivers?

Is it true that real estate prices mainly go up?

Why does Deadpool say "You're welcome, Canada," after shooting Ryan Reynolds in the end credits?

Provisioning profile doesn't include the application-identifier and keychain-access-groups entitlements

Where is the 1/8 CR apprentice in Volo's Guide to Monsters?

How to answer questions about my characters?

Font with correct density?

Can anyone tell me why this program fails?

Why did it take so long to abandon sail after steamships were demonstrated?

Why are the outputs of printf and std::cout different

Meaning of "SEVERA INDEOVI VAS" from 3rd Century slab

Does the statement `int val = (++i > ++j) ? ++i : ++j;` invoke undefined behavior?

How to simplify this time periods definition interface?

Should we release the security issues we found in our product as CVE or we can just update those on weekly release notes?

Can unconscious characters be unwilling?



Does the statement `int val = (++i > ++j) ? ++i : ++j;` invoke undefined behavior?



2019 Community Moderator Electionsequence points in cWhy are these constructs using pre and post-increment undefined behavior?Undefined, unspecified and implementation-defined behaviorIn C is there any guarantee with code prior to undefined behavior?Undefined behavior and sequence pointsUndefined behavior and sequence points reloadedIs the behavior of i = post_increment_i() specified, unspecified, or undefined?Undefined behavior and sequence pointUndefined behavior with: c = (b=a+2) - (a=1) ;Does a[a[0]] = 1 produce undefined behavior?printf with consecutive function as argument, undefined behavior










8















Given the following program:



#include <stdio.h>
int main(void)

int i = 1, j = 2;
int val = (++i > ++j) ? ++i : ++j;
printf("%dn", val); // prints 4
return 0;



The initialization of val seems like it could be hiding some undefined behavior, but I don't see any point at which an object is either modified more than once or modified and used without a sequence point in between. Could someone
either correct or corroborate me on this?










share|improve this question



















  • 3





    Is there a sequence point? Please see this answer which states there is one "Between the evaluations of the first operand of the conditional ?: operator and whichever of the second and third operands is evaluated (6.5.15)."

    – Weather Vane
    2 hours ago











  • You got 4. What did you expect?

    – Bob Jarvis
    2 hours ago











  • I expected 4. I don't think this code invokes UB, but I was told on another question that it does. Just wanted to eliminate any confusion as to whether that specific statement causes UB, and maybe get a better explanation that the one I provided in the question.

    – max1000001
    2 hours ago











  • the ternary expression guarantees the sequence point. I cannot find any reference to (++i > ++j) though. Is > a sequence point?

    – Jean-François Fabre
    2 hours ago






  • 2





    @Jean-FrançoisFabre No, but it doesn't need to be. There's no need for a sequence point between two changes of two different variables. ++i > ++i would be UB though.

    – sepp2k
    2 hours ago















8















Given the following program:



#include <stdio.h>
int main(void)

int i = 1, j = 2;
int val = (++i > ++j) ? ++i : ++j;
printf("%dn", val); // prints 4
return 0;



The initialization of val seems like it could be hiding some undefined behavior, but I don't see any point at which an object is either modified more than once or modified and used without a sequence point in between. Could someone
either correct or corroborate me on this?










share|improve this question



















  • 3





    Is there a sequence point? Please see this answer which states there is one "Between the evaluations of the first operand of the conditional ?: operator and whichever of the second and third operands is evaluated (6.5.15)."

    – Weather Vane
    2 hours ago











  • You got 4. What did you expect?

    – Bob Jarvis
    2 hours ago











  • I expected 4. I don't think this code invokes UB, but I was told on another question that it does. Just wanted to eliminate any confusion as to whether that specific statement causes UB, and maybe get a better explanation that the one I provided in the question.

    – max1000001
    2 hours ago











  • the ternary expression guarantees the sequence point. I cannot find any reference to (++i > ++j) though. Is > a sequence point?

    – Jean-François Fabre
    2 hours ago






  • 2





    @Jean-FrançoisFabre No, but it doesn't need to be. There's no need for a sequence point between two changes of two different variables. ++i > ++i would be UB though.

    – sepp2k
    2 hours ago













8












8








8








Given the following program:



#include <stdio.h>
int main(void)

int i = 1, j = 2;
int val = (++i > ++j) ? ++i : ++j;
printf("%dn", val); // prints 4
return 0;



The initialization of val seems like it could be hiding some undefined behavior, but I don't see any point at which an object is either modified more than once or modified and used without a sequence point in between. Could someone
either correct or corroborate me on this?










share|improve this question
















Given the following program:



#include <stdio.h>
int main(void)

int i = 1, j = 2;
int val = (++i > ++j) ? ++i : ++j;
printf("%dn", val); // prints 4
return 0;



The initialization of val seems like it could be hiding some undefined behavior, but I don't see any point at which an object is either modified more than once or modified and used without a sequence point in between. Could someone
either correct or corroborate me on this?







c ternary-operator sequence-points






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago









machine_1

2,47921232




2,47921232










asked 2 hours ago









max1000001max1000001

988




988







  • 3





    Is there a sequence point? Please see this answer which states there is one "Between the evaluations of the first operand of the conditional ?: operator and whichever of the second and third operands is evaluated (6.5.15)."

    – Weather Vane
    2 hours ago











  • You got 4. What did you expect?

    – Bob Jarvis
    2 hours ago











  • I expected 4. I don't think this code invokes UB, but I was told on another question that it does. Just wanted to eliminate any confusion as to whether that specific statement causes UB, and maybe get a better explanation that the one I provided in the question.

    – max1000001
    2 hours ago











  • the ternary expression guarantees the sequence point. I cannot find any reference to (++i > ++j) though. Is > a sequence point?

    – Jean-François Fabre
    2 hours ago






  • 2





    @Jean-FrançoisFabre No, but it doesn't need to be. There's no need for a sequence point between two changes of two different variables. ++i > ++i would be UB though.

    – sepp2k
    2 hours ago












  • 3





    Is there a sequence point? Please see this answer which states there is one "Between the evaluations of the first operand of the conditional ?: operator and whichever of the second and third operands is evaluated (6.5.15)."

    – Weather Vane
    2 hours ago











  • You got 4. What did you expect?

    – Bob Jarvis
    2 hours ago











  • I expected 4. I don't think this code invokes UB, but I was told on another question that it does. Just wanted to eliminate any confusion as to whether that specific statement causes UB, and maybe get a better explanation that the one I provided in the question.

    – max1000001
    2 hours ago











  • the ternary expression guarantees the sequence point. I cannot find any reference to (++i > ++j) though. Is > a sequence point?

    – Jean-François Fabre
    2 hours ago






  • 2





    @Jean-FrançoisFabre No, but it doesn't need to be. There's no need for a sequence point between two changes of two different variables. ++i > ++i would be UB though.

    – sepp2k
    2 hours ago







3




3





Is there a sequence point? Please see this answer which states there is one "Between the evaluations of the first operand of the conditional ?: operator and whichever of the second and third operands is evaluated (6.5.15)."

– Weather Vane
2 hours ago





Is there a sequence point? Please see this answer which states there is one "Between the evaluations of the first operand of the conditional ?: operator and whichever of the second and third operands is evaluated (6.5.15)."

– Weather Vane
2 hours ago













You got 4. What did you expect?

– Bob Jarvis
2 hours ago





You got 4. What did you expect?

– Bob Jarvis
2 hours ago













I expected 4. I don't think this code invokes UB, but I was told on another question that it does. Just wanted to eliminate any confusion as to whether that specific statement causes UB, and maybe get a better explanation that the one I provided in the question.

– max1000001
2 hours ago





I expected 4. I don't think this code invokes UB, but I was told on another question that it does. Just wanted to eliminate any confusion as to whether that specific statement causes UB, and maybe get a better explanation that the one I provided in the question.

– max1000001
2 hours ago













the ternary expression guarantees the sequence point. I cannot find any reference to (++i > ++j) though. Is > a sequence point?

– Jean-François Fabre
2 hours ago





the ternary expression guarantees the sequence point. I cannot find any reference to (++i > ++j) though. Is > a sequence point?

– Jean-François Fabre
2 hours ago




2




2





@Jean-FrançoisFabre No, but it doesn't need to be. There's no need for a sequence point between two changes of two different variables. ++i > ++i would be UB though.

– sepp2k
2 hours ago





@Jean-FrançoisFabre No, but it doesn't need to be. There's no need for a sequence point between two changes of two different variables. ++i > ++i would be UB though.

– sepp2k
2 hours ago












3 Answers
3






active

oldest

votes


















8














The behavior of this code is well defined.



The first expression in a conditional is guaranteed to be evaluated before either the second expression or the third expression, and only one of the second or third will be evaluated. This is described in section 6.5.15p4 of the C standard:




The first operand is evaluated; there is a sequence point
between its evaluation and the evaluation of the second or third
operand (whichever is evaluated). The second operand is evaluated
only if the first compares unequal to 0; the third operand is
evaluated only if the first compares equal to 0; the result is
the value of the second or third operand (whichever is
evaluated), converted to the type described below.




In the case of your expression:



int val = (++i > ++j) ? ++i : ++j;


++i > ++j is evaluated first. The incremented values of i and j are used in the comparison, so it becomes 2 > 3. The result is false, so then ++j is evaluated and ++i is not. So the (again) incremented value of j (i.e. 4) is then assigned to val.






share|improve this answer


















  • 1





    Thank you for your detailed and direct answer!

    – max1000001
    2 hours ago


















3














too late, but maybe useful.



(++i > ++j) ? ++i : ++j;


In the document ISO/IEC 9899:201xAnnex C(informative)Sequence points we find that there is a sequence point




Between the evaluations of the first operand of the conditional ?: operator and whichever of the second and third operands is evaluated




In order to be well defined behavior one must not modify 2 times (via side-effects) the same object between 2 sequence points.



In your expression the only conflict that could appear would be between the first and second ++i or ++j.



At every sequence point the value last stored in the object shall agree with that prescribed by the abstract machine (this is what you would compute on paper, like on a turing machine).



Quote from 5.1.2.3p3 Program execution




The presence of a sequence point between the evaluation of expressions A and B implies that every value computation and side effect associated with A is sequenced before every value computation and side effect associated with B.




When you have side-effects in your code, they are sequenced by different expressions. The rule says that between 2 sequence points you can permute these expressions as you wish.



For example. i = i++. Because none of the operators involved in this expression represent sequence points, you can permute the expressions that are side-effects as you want. The C language allows you to use any of these sequences



i = i; i = i+1; or i = i+1; i=i; or anything that provides the same result as the abstract semantics of computation asks for interpretation of this computation. The Standard ISO9899 defines the C language as abstract semantics.






share|improve this answer

























  • I think the part where you enumerate the modifications to i and j as "possible conflicts" adds something new and useful to the analysis. I hadn't thought of that, thanks!

    – max1000001
    1 hour ago


















2














There may be no UB in your program, but in the question:
Does the statement int val = (++i > ++j) ? ++i : ++j; invoke undefined behavior?



The answer is yes. Either or both of the increment operations may overflow, since i and j are signed, in which case all bets are off.



Of course this doesn't happen in your full example because you've specified the values as small integers.






share|improve this answer























  • I assure you that the question is not about signed integer overflow. It's about whether there is a sequence point between the first operand of the ternary operator and whichever wins from the second and the third operands.

    – machine_1
    3 mins ago










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',
autoActivateHeartbeat: false,
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%2f55170504%2fdoes-the-statement-int-val-i-j-i-j-invoke-undefined-behavio%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









8














The behavior of this code is well defined.



The first expression in a conditional is guaranteed to be evaluated before either the second expression or the third expression, and only one of the second or third will be evaluated. This is described in section 6.5.15p4 of the C standard:




The first operand is evaluated; there is a sequence point
between its evaluation and the evaluation of the second or third
operand (whichever is evaluated). The second operand is evaluated
only if the first compares unequal to 0; the third operand is
evaluated only if the first compares equal to 0; the result is
the value of the second or third operand (whichever is
evaluated), converted to the type described below.




In the case of your expression:



int val = (++i > ++j) ? ++i : ++j;


++i > ++j is evaluated first. The incremented values of i and j are used in the comparison, so it becomes 2 > 3. The result is false, so then ++j is evaluated and ++i is not. So the (again) incremented value of j (i.e. 4) is then assigned to val.






share|improve this answer


















  • 1





    Thank you for your detailed and direct answer!

    – max1000001
    2 hours ago















8














The behavior of this code is well defined.



The first expression in a conditional is guaranteed to be evaluated before either the second expression or the third expression, and only one of the second or third will be evaluated. This is described in section 6.5.15p4 of the C standard:




The first operand is evaluated; there is a sequence point
between its evaluation and the evaluation of the second or third
operand (whichever is evaluated). The second operand is evaluated
only if the first compares unequal to 0; the third operand is
evaluated only if the first compares equal to 0; the result is
the value of the second or third operand (whichever is
evaluated), converted to the type described below.




In the case of your expression:



int val = (++i > ++j) ? ++i : ++j;


++i > ++j is evaluated first. The incremented values of i and j are used in the comparison, so it becomes 2 > 3. The result is false, so then ++j is evaluated and ++i is not. So the (again) incremented value of j (i.e. 4) is then assigned to val.






share|improve this answer


















  • 1





    Thank you for your detailed and direct answer!

    – max1000001
    2 hours ago













8












8








8







The behavior of this code is well defined.



The first expression in a conditional is guaranteed to be evaluated before either the second expression or the third expression, and only one of the second or third will be evaluated. This is described in section 6.5.15p4 of the C standard:




The first operand is evaluated; there is a sequence point
between its evaluation and the evaluation of the second or third
operand (whichever is evaluated). The second operand is evaluated
only if the first compares unequal to 0; the third operand is
evaluated only if the first compares equal to 0; the result is
the value of the second or third operand (whichever is
evaluated), converted to the type described below.




In the case of your expression:



int val = (++i > ++j) ? ++i : ++j;


++i > ++j is evaluated first. The incremented values of i and j are used in the comparison, so it becomes 2 > 3. The result is false, so then ++j is evaluated and ++i is not. So the (again) incremented value of j (i.e. 4) is then assigned to val.






share|improve this answer













The behavior of this code is well defined.



The first expression in a conditional is guaranteed to be evaluated before either the second expression or the third expression, and only one of the second or third will be evaluated. This is described in section 6.5.15p4 of the C standard:




The first operand is evaluated; there is a sequence point
between its evaluation and the evaluation of the second or third
operand (whichever is evaluated). The second operand is evaluated
only if the first compares unequal to 0; the third operand is
evaluated only if the first compares equal to 0; the result is
the value of the second or third operand (whichever is
evaluated), converted to the type described below.




In the case of your expression:



int val = (++i > ++j) ? ++i : ++j;


++i > ++j is evaluated first. The incremented values of i and j are used in the comparison, so it becomes 2 > 3. The result is false, so then ++j is evaluated and ++i is not. So the (again) incremented value of j (i.e. 4) is then assigned to val.







share|improve this answer












share|improve this answer



share|improve this answer










answered 2 hours ago









dbushdbush

102k13105143




102k13105143







  • 1





    Thank you for your detailed and direct answer!

    – max1000001
    2 hours ago












  • 1





    Thank you for your detailed and direct answer!

    – max1000001
    2 hours ago







1




1





Thank you for your detailed and direct answer!

– max1000001
2 hours ago





Thank you for your detailed and direct answer!

– max1000001
2 hours ago













3














too late, but maybe useful.



(++i > ++j) ? ++i : ++j;


In the document ISO/IEC 9899:201xAnnex C(informative)Sequence points we find that there is a sequence point




Between the evaluations of the first operand of the conditional ?: operator and whichever of the second and third operands is evaluated




In order to be well defined behavior one must not modify 2 times (via side-effects) the same object between 2 sequence points.



In your expression the only conflict that could appear would be between the first and second ++i or ++j.



At every sequence point the value last stored in the object shall agree with that prescribed by the abstract machine (this is what you would compute on paper, like on a turing machine).



Quote from 5.1.2.3p3 Program execution




The presence of a sequence point between the evaluation of expressions A and B implies that every value computation and side effect associated with A is sequenced before every value computation and side effect associated with B.




When you have side-effects in your code, they are sequenced by different expressions. The rule says that between 2 sequence points you can permute these expressions as you wish.



For example. i = i++. Because none of the operators involved in this expression represent sequence points, you can permute the expressions that are side-effects as you want. The C language allows you to use any of these sequences



i = i; i = i+1; or i = i+1; i=i; or anything that provides the same result as the abstract semantics of computation asks for interpretation of this computation. The Standard ISO9899 defines the C language as abstract semantics.






share|improve this answer

























  • I think the part where you enumerate the modifications to i and j as "possible conflicts" adds something new and useful to the analysis. I hadn't thought of that, thanks!

    – max1000001
    1 hour ago















3














too late, but maybe useful.



(++i > ++j) ? ++i : ++j;


In the document ISO/IEC 9899:201xAnnex C(informative)Sequence points we find that there is a sequence point




Between the evaluations of the first operand of the conditional ?: operator and whichever of the second and third operands is evaluated




In order to be well defined behavior one must not modify 2 times (via side-effects) the same object between 2 sequence points.



In your expression the only conflict that could appear would be between the first and second ++i or ++j.



At every sequence point the value last stored in the object shall agree with that prescribed by the abstract machine (this is what you would compute on paper, like on a turing machine).



Quote from 5.1.2.3p3 Program execution




The presence of a sequence point between the evaluation of expressions A and B implies that every value computation and side effect associated with A is sequenced before every value computation and side effect associated with B.




When you have side-effects in your code, they are sequenced by different expressions. The rule says that between 2 sequence points you can permute these expressions as you wish.



For example. i = i++. Because none of the operators involved in this expression represent sequence points, you can permute the expressions that are side-effects as you want. The C language allows you to use any of these sequences



i = i; i = i+1; or i = i+1; i=i; or anything that provides the same result as the abstract semantics of computation asks for interpretation of this computation. The Standard ISO9899 defines the C language as abstract semantics.






share|improve this answer

























  • I think the part where you enumerate the modifications to i and j as "possible conflicts" adds something new and useful to the analysis. I hadn't thought of that, thanks!

    – max1000001
    1 hour ago













3












3








3







too late, but maybe useful.



(++i > ++j) ? ++i : ++j;


In the document ISO/IEC 9899:201xAnnex C(informative)Sequence points we find that there is a sequence point




Between the evaluations of the first operand of the conditional ?: operator and whichever of the second and third operands is evaluated




In order to be well defined behavior one must not modify 2 times (via side-effects) the same object between 2 sequence points.



In your expression the only conflict that could appear would be between the first and second ++i or ++j.



At every sequence point the value last stored in the object shall agree with that prescribed by the abstract machine (this is what you would compute on paper, like on a turing machine).



Quote from 5.1.2.3p3 Program execution




The presence of a sequence point between the evaluation of expressions A and B implies that every value computation and side effect associated with A is sequenced before every value computation and side effect associated with B.




When you have side-effects in your code, they are sequenced by different expressions. The rule says that between 2 sequence points you can permute these expressions as you wish.



For example. i = i++. Because none of the operators involved in this expression represent sequence points, you can permute the expressions that are side-effects as you want. The C language allows you to use any of these sequences



i = i; i = i+1; or i = i+1; i=i; or anything that provides the same result as the abstract semantics of computation asks for interpretation of this computation. The Standard ISO9899 defines the C language as abstract semantics.






share|improve this answer















too late, but maybe useful.



(++i > ++j) ? ++i : ++j;


In the document ISO/IEC 9899:201xAnnex C(informative)Sequence points we find that there is a sequence point




Between the evaluations of the first operand of the conditional ?: operator and whichever of the second and third operands is evaluated




In order to be well defined behavior one must not modify 2 times (via side-effects) the same object between 2 sequence points.



In your expression the only conflict that could appear would be between the first and second ++i or ++j.



At every sequence point the value last stored in the object shall agree with that prescribed by the abstract machine (this is what you would compute on paper, like on a turing machine).



Quote from 5.1.2.3p3 Program execution




The presence of a sequence point between the evaluation of expressions A and B implies that every value computation and side effect associated with A is sequenced before every value computation and side effect associated with B.




When you have side-effects in your code, they are sequenced by different expressions. The rule says that between 2 sequence points you can permute these expressions as you wish.



For example. i = i++. Because none of the operators involved in this expression represent sequence points, you can permute the expressions that are side-effects as you want. The C language allows you to use any of these sequences



i = i; i = i+1; or i = i+1; i=i; or anything that provides the same result as the abstract semantics of computation asks for interpretation of this computation. The Standard ISO9899 defines the C language as abstract semantics.







share|improve this answer














share|improve this answer



share|improve this answer








edited 51 mins ago

























answered 1 hour ago









alinsoaralinsoar

8,75213251




8,75213251












  • I think the part where you enumerate the modifications to i and j as "possible conflicts" adds something new and useful to the analysis. I hadn't thought of that, thanks!

    – max1000001
    1 hour ago

















  • I think the part where you enumerate the modifications to i and j as "possible conflicts" adds something new and useful to the analysis. I hadn't thought of that, thanks!

    – max1000001
    1 hour ago
















I think the part where you enumerate the modifications to i and j as "possible conflicts" adds something new and useful to the analysis. I hadn't thought of that, thanks!

– max1000001
1 hour ago





I think the part where you enumerate the modifications to i and j as "possible conflicts" adds something new and useful to the analysis. I hadn't thought of that, thanks!

– max1000001
1 hour ago











2














There may be no UB in your program, but in the question:
Does the statement int val = (++i > ++j) ? ++i : ++j; invoke undefined behavior?



The answer is yes. Either or both of the increment operations may overflow, since i and j are signed, in which case all bets are off.



Of course this doesn't happen in your full example because you've specified the values as small integers.






share|improve this answer























  • I assure you that the question is not about signed integer overflow. It's about whether there is a sequence point between the first operand of the ternary operator and whichever wins from the second and the third operands.

    – machine_1
    3 mins ago















2














There may be no UB in your program, but in the question:
Does the statement int val = (++i > ++j) ? ++i : ++j; invoke undefined behavior?



The answer is yes. Either or both of the increment operations may overflow, since i and j are signed, in which case all bets are off.



Of course this doesn't happen in your full example because you've specified the values as small integers.






share|improve this answer























  • I assure you that the question is not about signed integer overflow. It's about whether there is a sequence point between the first operand of the ternary operator and whichever wins from the second and the third operands.

    – machine_1
    3 mins ago













2












2








2







There may be no UB in your program, but in the question:
Does the statement int val = (++i > ++j) ? ++i : ++j; invoke undefined behavior?



The answer is yes. Either or both of the increment operations may overflow, since i and j are signed, in which case all bets are off.



Of course this doesn't happen in your full example because you've specified the values as small integers.






share|improve this answer













There may be no UB in your program, but in the question:
Does the statement int val = (++i > ++j) ? ++i : ++j; invoke undefined behavior?



The answer is yes. Either or both of the increment operations may overflow, since i and j are signed, in which case all bets are off.



Of course this doesn't happen in your full example because you've specified the values as small integers.







share|improve this answer












share|improve this answer



share|improve this answer










answered 1 hour ago









Doug CurrieDoug Currie

35.8k77109




35.8k77109












  • I assure you that the question is not about signed integer overflow. It's about whether there is a sequence point between the first operand of the ternary operator and whichever wins from the second and the third operands.

    – machine_1
    3 mins ago

















  • I assure you that the question is not about signed integer overflow. It's about whether there is a sequence point between the first operand of the ternary operator and whichever wins from the second and the third operands.

    – machine_1
    3 mins ago
















I assure you that the question is not about signed integer overflow. It's about whether there is a sequence point between the first operand of the ternary operator and whichever wins from the second and the third operands.

– machine_1
3 mins ago





I assure you that the question is not about signed integer overflow. It's about whether there is a sequence point between the first operand of the ternary operator and whichever wins from the second and the third operands.

– machine_1
3 mins ago

















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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55170504%2fdoes-the-statement-int-val-i-j-i-j-invoke-undefined-behavio%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

Best approach to update all entries in a list that is paginated?Best way to add items to a paginated listChoose Your Country: Best Usability approachUpdate list when a user is viewing the list without annoying themWhen would the best day to update your webpage be?What should happen when I add a Row to a paginated, sorted listShould I adopt infinite scrolling or classical pagination?How to show user that page objects automatically updateWhat is the best location to locate the comments section in a list pageBest way to combine filtering and selecting items in a listWhen one of two inputs must be updated to satisfy a consistency criteria, which should you update (if at all)?

Вунгтау (аеропорт) Загальні відомості | Див. також | Посилання | Навігаційне меню10°22′00″ пн. ш. 107°05′00″ сх. д. / 10.36667° пн. ш. 107.08333° сх. д. / 10.36667; 107.0833310°22′00″ пн. ш. 107°05′00″ сх. д. / 10.36667° пн. ш. 107.08333° сх. д. / 10.36667; 107.083337731608Vinh AirportVinh airport facelift improves serviceвиправивши або дописавши їївиправивши або дописавши їїр

Тонконіг бульбистий Зміст Опис | Поширення | Екологія | Господарське значення | Примітки | Див. також | Література | Джерела | Посилання | Навігаційне меню1114601320038-241116202404kew-435458Poa bulbosaЭлектронный каталог сосудистых растений Азиатской России [Електронний каталог судинних рослин Азіатської Росії]Малышев Л. Л. Дикие родичи культурных растений. Poa bulbosa L. - Мятлик луковичный. [Малишев Л. Л. Дикі родичи культурних рослин. Poa bulbosa L. - Тонконіг бульбистий.]Мятлик (POA) Сем. Злаки (Мятликовые) [Тонконіг (POA) Род. Злаки (Тонконогові)]Poa bulbosa Linnaeus, Sp. Pl. 1: 70. 1753. 鳞茎早熟禾 lin jing zao shu he (Description from Flora of China) [Poa bulbosa Linnaeus, Sp. Pl. 1: 70. 1753. 鳞茎早熟禾 lin jing zao shu he (Опис від Флора Китаю)]Poa bulbosa L. – lipnice cibulkatá / lipnica cibulkatáPoa bulbosa в базі даних Poa bulbosa на сайті Poa bulbosa в базі даних «Global Biodiversity Information Facility» (GBIF)Poa bulbosa в базі даних «Euro + Med PlantBase» — інформаційному ресурсі для Євро-середземноморського розмаїття рослинPoa bulbosa L. на сайті «Плантариум»