Search code examples
c++compiler-errorscomma-operator

Why does the following code not produce a compilation error?


I am using VS2005 compiler and I am expecting following code to give compilation error.

int a=1, b= 2, c=3;
a = (b,c);

value of a after assignment is 3. As per my understanding it should give compilation error.

I would be happy to know if there is any valid reason behind this.


Solution

  • You are using the comma operator in C++, it is not commonly used. This works as follows

    <expression1>, <expression2>
    

    It evaluates <expression1> and discards the results and then evaluates <expression2> and takes the result of that is returned as the value of the whole expression.