-2

I am trying to understand how the following lines of code work in c++.

int main(){

    int i;
    i = 1 + (2,3,5,3,6);
    cout<<i<<endl;
    return 0;
}

Output: 7 Basically, the answer is the sum of 1 and the last integer in between the parentheses.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    [A good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) should have told you about the [built-in comma operator](https://en.cppreference.com/w/cpp/language/operator_other#Built-in_comma_operator). – Some programmer dude Aug 09 '19 at 05:42

1 Answers1

0

(2,3,5,3,6) turns out to be 6. Hence 1 + 6 = 7

You can verify with a print statement

printf("\n%d\n", (2,3,5,3,6));

It will print 6 only.

Austin
  • 1,709
  • 20
  • 40