Evaluation order of operands in C++



There are some rules in programming that govern how an operation is performed.

The order of evaluation of operation and the associativity of operations (which is left to right is defined).

Here is a program to show the evaluation order of operands,

Example

Live Demo

#include <iostream>
using namespace std;
int x = 2;

int changeVal() {
   x *= x;
   return x;
}

int main() {
   
   int p = changeVal() + changeVal();
   cout<<"Value: "<<x<<endl;
   cout<<"Operation result: "<<p<<endl;
   return 0;
}

Output −

Value: 16
Operation result: 20

Advertisements