Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the post-increment in function arguments

Tags:

c++

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++);
}
like image 571
ordinary Avatar asked Jun 16 '12 05:06

ordinary


People also ask

What is i ++ and ++ i difference?

i++ : gets the element and then increments it. ++i : increments i and then returns the element. Example: int i = 0; printf("i: %d\n", i); printf("i++: %d\n", i++); printf("++i: %d\n", ++i); Output: i: 0 i++: 0 ++i: 2.

Can you increment a function in C++?

In C++, an increment operator is used to increment the value of the variable by 1. The symbol ++ is used to represent the increment operator.

How does post increment work?

2) Post-increment operator: A post-increment operator is used to increment the value of the variable after executing the expression completely in which post-increment is used. In the Post-Increment, value is first used in an expression and then incremented.

What is post increment operator in C++?

Post-increment (i++) − After assigning the value to the variable, the value is incremented. The following is the syntax of pre and post increment. ++variable_name; // Pre-increment variable_name++; // Post-increment. Here, variable_name − Any name of the variable given by user.


1 Answers

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.

like image 88
Paul Avatar answered Dec 26 '22 02:12

Paul