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

  • Home
  • SEARCH
  • 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 8087191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:41:43+00:00 2026-06-05T18:41:43+00:00

When I run this code, the output is 11, 10. Why on earth would

  • 0

When I run this code, the output is 11, 10.

Why on earth would that be? Can someone give me an explanation of this that will hopefully enlighten me?

Thanks

#include <iostream>
using namespace std;

void print(int x, int y)
{ 
    cout << x << endl;
    cout << y << endl;
}
int main()
{
    int x = 10;
    print(x, x++);
}
  • 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-06-05T18:41:45+00:00Added an answer on June 5, 2026 at 6:41 pm

    The C++ standard states (A note in section 1.9.16):

    Value computations and side effects associated with the different argument expressions are unsequenced.

    In other words, it’s undefined and/or compiler-dependent which order the arguments are evaluated in before their value is passed into the function. So on some compilers (which evaluate the left argument first) that code would output 10, 10 and on others (which evaluate the right argument first) it will output 11, 10. In general you should never rely on undefined behaviour.

    To help you understand this, imagine that each argument expression is evaluated before the function is called like so (not that this is exactly how it actually works, it’s just an easy way to think of it that will help you understand the sequencing):

    int arg1 = x;       // This line
    int arg2 = x++;     // And this line can be swapped.
    print(arg1, arg2);
    

    The C++ Standard says that the two argument expression are unsequenced. So, if we write out the argument expressions on separate lines like this, their order should not be significant, because the standard says they can be evaluated in any order. Some compilers might evaluate them in the order above, others might swap them:

    int arg2 = x++;     // And this line can be swapped.
    int arg1 = x;       // This line
    print(arg1, arg2);
    

    That makes it pretty obvious how arg2 can hold the value 10, while arg1 holds the value 11.

    You should always avoid this undefined behaviour in your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I run this code gcc gives me the output 10. Can someone explain
When I run this code url = ('http://maps.google.com/maps/nav?'+ 'q=from%3A'+from_address+ '+to%3A'+to_address+ '&output=json&oe=utf8&key='+api_key) request = urllib2.Request(url)
If I run this code, will each AppDomain execute in a different thread? ThreadPool.QueueUserWorkItem(delegate
Please run this code, and explain me why the output is like: table[0]=3 table[1]=-858993567
When I run this code, the number at the top of the output window
Can anyone explain why the code below, when run, will occasionally result in some
Why this code don't work,when i want run this code vwd 2008 express show
When I run this code, About half-way through the concatenation loop, $xml becomes null
I'm trying to run this code: let coins = [50, 25, 10, 5, 2,1]
On document ready I run this code: jQuery(document).ready(function(){ jQuery('#button').click(function() { jQuery('#contact_form').load(/Users/mge/Downloads/jquery-ajax-1/readme.txt); return false; });

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.