-9
#include<iostream>
int main()
{ 
    int a = 5;
    a = (a = 10, a++, a--);
    std::cout << a;

}

Output is 11, But when I modify the line

a = a=10,a++,a--;

Output is 10 What effect does removing the ( ) operator has and in what order the operators are being executed.

yizzlez
  • 8,757
  • 4
  • 29
  • 44
Roh777
  • 1
  • 1
  • 1
  • Comma has [lowest precedence](http://stackoverflow.com/a/18444099/1708801) – Shafik Yaghmour Jul 29 '15 at 18:42
  • @Rohit use compiler warnings `-Wall` – Steephen Jul 29 '15 at 18:42
  • Wait, what? undefined behavior? The comma operator introduces a sequence point @πάνταῥεῖ – Shafik Yaghmour Jul 29 '15 at 18:44
  • _What effect does removing the ( ) operator has and in what order the operators are being executed_ Better question: why are you so uninterested in learning these details that you would rather ask random people for help? Write some code and figure the answer out yourself. For self-bonus points, inspect the assembly code the compiler can generate (if you ask it to) and spend some time to understand it. – mah Jul 29 '15 at 18:44

2 Answers2

0

The + and - operators take precedence before the assignment operator and the +,- signs are read by the compiler from left to right, whereas the assignment operators are read by the compiler from right to left. So: 1) a++ 2) a-- 3) a=10 4) a =

timtoy1
  • 31
  • 5
0

The c++ compiler will pass your complete set code to a binary tree which will rearrange your code according to the precedence of the operator = + - etc in left node or right node