Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4096478
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:03:05+00:00 2026-05-20T20:03:05+00:00

Possible Duplicate: Undefined Behavior and Sequence Points In C++ on a machine code level,

  • 0

Possible Duplicate:
Undefined Behavior and Sequence Points

In C++ on a machine code level, when does the postincrement++ operator get executed?

The precedence table indicates that postfix++ operators are level 2: which means in

int x = 0 ;
int y = x++ + x++ ;  // ans: y=0

The postfix ++’s execute first.

However, it would seem that the logical operation of this line is the addition happens first (0+0), but how does that happen?

What I imagine, is the following:

// Option 1:
// Perform x++ 2 times.
// Each time you do x++, you change the value of x..
// but you "return" the old value of x there?
int y = 0 + x++ ;  // x becomes 1, 0 is "returned" from x++

// do it for the second one..
int y = 0 + 0 ;  // x becomes 2, 0 is "returned" from x++... but how?
// if this is really what happens, the x was already 1 right now.

So, the other option is although x++ is higher on the precedence table that x + x, the code generated due to x++ is inserted below the addition operation

// Option 2:  turn this into
int y = x + x ; // 
x++ ;
x++ ;

That second option seems to make more sense, but I’m interested in the order of operations here. Specifically, when does x change?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:03:06+00:00Added an answer on May 20, 2026 at 8:03 pm

    Instead of jumping on the details of the example that is UB, I will discuss the following example that is perfectly fine:

    int a = 0, b = 0;
    int c = a++ + b++;
    

    Now, the precedence of operators means that the last line is equivalent to:

    int c = (a++) + (b++);
    

    And not:

    int c = (a++ + b)++; // compile time error, post increment an rvalue
    

    On the other hand, the semantics of the post increment are equivalent to two separate instructions (from here on is just a mental picture):

    a++; // similar to: (__tmp = a, ++a, __tmp) 
         // -- ignoring the added sequence points of , here
    

    That is, the original expression will be interpreted by the compiler as:

    auto __tmp1 = a;         // 1
    auto __tmp2 = b;         // 2
    ++a;                     // 3
    ++b;                     // 4
    int c = __tmp1 + __tmp2; // 5
    

    But the compiler is allowed to reorder the 5 instructions as long as the following constraints are met (where x>y means x must be executed before y, or x precedes y):

    1 > 3        // cannot increment a before getting the old value
    2 > 4        // cannot increment b before getting the old value
    1 > 5, 2 > 5 // the sum cannot happen before both temporaries are created
    

    There are no other constraints in the order of execution of the different instructions, so the following are all valid sequences:

    1, 2, 3, 4, 5
    1, 2, 5, 3, 4
    1, 3, 2, 4, 5
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Pre & post increment operator behavior in C, C++, Java, & C#
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i ,
Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i ,
Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i ,
Possible Duplicate: Undefined reference - C++ linker error Now, I'm getting an Undefined reference
Possible Duplicate: Detecting an undefined object property in JavaScript From the below JavaScript sample,
Possible Duplicate: What is an undefined reference/unresolved external symbol error and how do I
Possible Duplicate: What is the !! (not not) operator in JavaScript? I've seen operator
Possible Duplicate: Detecting an undefined object property in JavaScript Would it be like this?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.