Cpp - What is the output: operator precedence?

Question

How are operands and operators in the following expression associated?

Insert parentheses to form equivalent expressions.


x = -4 * i++ - 6 % 4;  


Click to view the answer

x = ( ((-4) * (i++)) - (6 % 4) )

What value will be assigned in part a to the variable x if the variable i has a value of -2?



Click to view the answer

The value 6 will be assigned to the variable x.

Demo

#include <iostream> 
using namespace std;
int main()/*from w  w w. j a v  a  2  s.  c o m*/
{
  int i = -2;
  int x = -4 * i++ - 6 % 4;
  cout << x << endl;

  i = -2;
  x = (((-4) * (i++)) - (6 % 4));

  cout << x << endl;

  return 0;
}

Result

Related Exercise