Evaluation order of operands in C++

C++Server Side ProgrammingProgramming

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
raja
Published on 22-Jan-2021 17:43:09
Advertisements