-3

I found a usage of () in this code

    int a, b, c;
    int x = (a= 2,b=5, a+b,++b );
    cout<<x;

I can't find any exmaple in Microsoft Docs or other place. Have anyone tell me What is the role of ()?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
LIFUGUAN
  • 73
  • 6
  • 1
    Look at the [comma operator](https://en.cppreference.com/w/cpp/language/operator_other) – Fureeish Oct 16 '18 at 13:38
  • It's the same as for *any* expression. The important part is the use of the *comma expression* inside the parentheses. – Some programmer dude Oct 16 '18 at 13:38
  • 2
    Very similar to [return list of values between parenthesis \(10, 20, 30, 40)/?](https://stackoverflow.com/q/20001349/1708801) – Shafik Yaghmour Oct 16 '18 at 15:30
  • The upshot is that each of the comma statements will be performed and x will be assigned the value from the last one. The third one `a+b` seems dubious. – Gem Taylor Oct 16 '18 at 17:50

1 Answers1

1

This is just () around an expression.

The expression uses operator,. operator, takes the left hand argument, evaluates, it, discards it, then evaluates and returns the right hand argument. (Unless, of course, you overload it)

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524