<p>How does the comma operator work in C++?</p>

<p>For instance, if I do:</p>

<pre><code>a = b, c;  
</code></pre>

<p>Does a end up equaling b or c? </p>

<p>(Yes, I know this is easy to test - just documenting on here for someone to find the answer quickly.)</p>

<p><strong>Update:</strong>  This question has exposed a nuance when using the comma operator.  Just to document this:</p>

<pre><code>a = b, c;    // a is set to the value of b!

a = (b, c);  // a is set to the value of c!
</code></pre>

<p>This question was actually inspired by a typo in code.  What was intended to be</p>

<pre><code>a = b;
c = d;
</code></pre>

<p>Turned into</p>

<pre><code>a = b,    //  &lt;-  Note comma typo!
c = d;
</code></pre>

[{'answer_id': 54146, 'author': 'Leon Timmermans', 'author_id': 4727, 'author_profile': 'https://Stackoverflow.com/users/4727', 'pm_score': 7, 'selected': True, 'text': '<p>It would be equal to <code>b</code>.</p>\n\n<p>The comma operator has a lower precedence than assignment.</p>\n'}, {'answer_id': 54154, 'author': 'prakash', 'author_id': 123, 'author_profile': 'https://Stackoverflow.com/users/123', 'pm_score': 3, 'selected': False, 'text': "<p>b's value will be assigned to a.\nNothing will happen to c</p>\n"}, {'answer_id': 54172, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 7, 'selected': False, 'text': '<p>Take care to notice that the comma operator may be overloaded in C++. The actual behaviour may thus be very different from the one expected.</p>\n\n<p>As an example, <a href="http://spirit.sourceforge.net/" rel="noreferrer">Boost.Spirit</a> uses the comma operator quite cleverly to implement list initializers for symbol tables. Thus, it makes the following syntax possible and meaningful:</p>\n\n<pre><code>keywords = "and", "or", "not", "xor";\n</code></pre>\n\n<p>Notice that due to operator precedence, the code is (intentionally!) identical to</p>\n\n<pre><code>(((keywords = "and"), "or"), "not"), "xor";\n</code></pre>\n\n<p>That is, the first operator called is <code>keywords.operator =("and")</code> which returns a proxy object on which the remaining <code>operator,</code>s are invoked:</p>\n\n<pre><code>keywords.operator =("and").operator ,("or").operator ,("not").operator ,("xor");\n</code></pre>\n'}, {'answer_id': 54173, 'author': 'Jason Carreiro', 'author_id': 5582, 'author_profile': 'https://Stackoverflow.com/users/5582', 'pm_score': 2, 'selected': False, 'text': '<p>The value of a will be equal to b, since the comma operator has a lower precedence than the assignment operator.</p>\n'}, {'answer_id': 65438, 'author': 'MobyDX', 'author_id': 3923, 'author_profile': 'https://Stackoverflow.com/users/3923', 'pm_score': 5, 'selected': False, 'text': '<p>The value of <code>a</code> will be <code>b</code>, but the value of <em><strong>the expression</strong></em> will be <code>c</code>. That is, in</p>\n<pre><code>d = (a = b, c);\n</code></pre>\n<p><code>a</code> would be equal to <code>b</code>, and <code>d</code> would be equal to <code>c</code>.</p>\n'}, {'answer_id': 114404, 'author': 'efotinis', 'author_id': 12320, 'author_profile': 'https://Stackoverflow.com/users/12320', 'pm_score': 7, 'selected': False, 'text': '<p>The comma operator has the <strong>lowest</strong> precedence of all C/C++ operators. Therefore it\'s always the last one to bind to an expression, meaning this:</p>\n\n<pre><code>a = b, c;\n</code></pre>\n\n<p>is equivalent to:</p>\n\n<pre><code>(a = b), c;\n</code></pre>\n\n<p>Another interesting fact is that the comma operator introduces a <a href="http://en.wikipedia.org/wiki/Sequence_point" rel="noreferrer">sequence point</a>. This means that the expression:</p>\n\n<pre><code>a+b, c(), d\n</code></pre>\n\n<p>is guaranteed to have its three subexpressions (<strong>a+b</strong>, <strong>c()</strong> and <strong>d</strong>) evaluated in order. This is significant if they have side-effects. Normally compilers are allowed to evaluate subexpressions in whatever order they find fit; for example, in a function call:</p>\n\n<pre><code>someFunc(arg1, arg2, arg3)\n</code></pre>\n\n<p>arguments can be evaluated in an arbitrary order. Note that the commas in the function call are <em>not</em> operators; they are separators.</p>\n'}, {'answer_id': 15861440, 'author': 'Quonux', 'author_id': 388614, 'author_profile': 'https://Stackoverflow.com/users/388614', 'pm_score': -1, 'selected': False, 'text': '<p><strong>First things first:</strong> Comma is actually not an operator, for the compiler it is just a token which gets a meaning <em>in context</em> with other tokens.</p>\n\n<h2>What does this mean and why bother?</h2>\n\n<p><strong>Example 1:</strong></p>\n\n<p>To understand the difference between the meaning of the same token in a different context we take a look at this example:</p>\n\n<pre><code>class Example {\n   Foo&lt;int, char*&gt; ContentA;\n}\n</code></pre>\n\n<p>Usually a C++ beginner would think that this expression could/would compare things but it is absolutly wrong, the meaning of the <code>&lt;</code>, <code>&gt;</code> and <code>,</code> tokens depent on the context of use.</p>\n\n<p>The correct interpretation of the example above is of course that it is an instatiation of a template.</p>\n\n<p><strong>Example 2:</strong></p>\n\n<p>When we write a typically for loop with more than one initialisation variable and/or more than one expressions that should be done after each iteration of the loop we use comma too:</p>\n\n<pre><code>for(a=5,b=0;a&lt;42;a++,b--)\n   ...\n</code></pre>\n\n<p>The meaning of the comma depends on the context of use, here it is the context of the <code>for</code> construction.</p>\n\n<h2>What does a comma in context actually mean?</h2>\n\n<p>To complicate it even more (as always in C++) the comma operator can itself be overloaded (thanks to <a href="https://stackoverflow.com/users/1968/konrad-rudolph">Konrad Rudolph</a> for pointing that out).</p>\n\n<p>To come back to the question, the Code</p>\n\n<pre><code>a = b, c;\n</code></pre>\n\n<p>means for the compiler something like</p>\n\n<pre><code>(a = b), c;\n</code></pre>\n\n<p>because the <a href="http://en.cppreference.com/w/cpp/language/operator_precedence" rel="nofollow noreferrer">priority</a> of the <code>=</code> token/operator is higher than the priority of the <code>,</code> token.</p>\n\n<p>and this is interpreted in context like</p>\n\n<pre><code>a = b;\nc;\n</code></pre>\n\n<p>(note that the interpretation depend on context, here it it neither a function/method call or a template instatiation.)</p>\n'}, {'answer_id': 19198977, 'author': 'CygnusX1', 'author_id': 635654, 'author_profile': 'https://Stackoverflow.com/users/635654', 'pm_score': 6, 'selected': False, 'text': '<p>The comma operator:</p>\n\n<ul>\n<li>has the lowest precedence</li>\n<li>is left-associative</li>\n</ul>\n\n<p>A default version of comma operator is defined for all types (built-in and custom), and it works as follows - given <code>exprA , exprB</code>:</p>\n\n<ul>\n<li><code>exprA</code> is evaluated</li>\n<li>the result of <code>exprA</code> is ignored</li>\n<li><code>exprB</code> is evaluated</li>\n<li>the result of <code>exprB</code> is returned as the result of the whole expression</li>\n</ul>\n\n<p>With most operators, the compiler is allowed to choose the order of execution and it is even required to skip the execution whatsoever if it does not affect the final result (e.g. <code>false &amp;&amp; foo()</code> will skip the call to <code>foo</code>). This is however not the case for comma operator and the above steps will always happen<sup>*</sup>.</p>\n\n<p>In practice, the default comma operator works almost the same way as a semicolon. The difference is that two expressions separated by a semicolon form two separate statements, while comma-separation keeps all as a single expression. This is why comma operator is sometimes used in the following scenarios:</p>\n\n<ul>\n<li>C syntax requires an single <em>expression</em>, not a statement. e.g. in <code>if( HERE )</code></li>\n<li>C syntax requires a single statement, not more, e.g. in the initialization of the <code>for</code> loop <code>for ( HERE ; ; )</code></li>\n<li>When you want to skip curly braces and keep a single statement: <code>if (foo) HERE ;</code> (please don\'t do that, it\'s really ugly!)</li>\n</ul>\n\n<p>When a statement is not an expression, semicolon cannot be replaced by a comma. For example these are disallowed:</p>\n\n<ul>\n<li><code>(foo, if (foo) bar)</code> (<code>if</code> is not an expression)</li>\n<li>int x, int y (variable declaration is not an expression)</li>\n</ul>\n\n<hr>\n\n<p>In your case we have:</p>\n\n<ul>\n<li><code>a=b, c;</code>, equivalent to <code>a=b; c;</code>, assuming that <code>a</code> is of type that does not overload the comma operator.</li>\n<li><code>a = b, c = d;</code> equivalent to <code>a=b; c=d;</code>, assuming that <code>a</code> is of type that does not overload the comma operator.</li>\n</ul>\n\n<hr>\n\n<p>Do note that not every comma is actually a comma operator. Some commas which have a completely different meaning:</p>\n\n<ul>\n<li><code>int a, b;</code> --- variable declaration list is comma separated, but these are not comma operators</li>\n<li><code>int a=5, b=3;</code> --- this is also a comma separated variable declaration list</li>\n<li><code>foo(x,y)</code> --- comma-separated argument list. In fact, <code>x</code> and <code>y</code> can be evaluated in <em>any</em> order!</li>\n<li><code>FOO(x,y)</code> --- comma-separated macro argument list</li>\n<li><code>foo&lt;a,b&gt;</code> --- comma-separated template argument list</li>\n<li><code>int foo(int a, int b)</code> --- comma-separated parameter list</li>\n<li><code>Foo::Foo() : a(5), b(3) {}</code> --- comma-separated initializer list in a class constructor</li>\n</ul>\n\n<hr>\n\n<p><sup>*</sup> This is not entirely true if you apply optimizations. If the compiler recognizes that certain piece of code has absolutely no impact on the rest, it will remove the unnecessary statements.</p>\n\n<p>Further reading: <a href="http://en.wikipedia.org/wiki/Comma_operator">http://en.wikipedia.org/wiki/Comma_operator</a></p>\n'}, {'answer_id': 31221185, 'author': 'Roopam', 'author_id': 3529776, 'author_profile': 'https://Stackoverflow.com/users/3529776', 'pm_score': 2, 'selected': False, 'text': '<p>Yes Comma operator has low precedence than Assignment operator</p>\n\n<pre><code>#include&lt;stdio.h&gt;\nint main()\n{\n          int i;\n          i = (1,2,3);\n          printf("i:%d\\n",i);\n          return 0;\n}\n</code></pre>\n\n<p>Output : i=3<br>\nBecause comma operator always return rightmost value.<br>\nIn case of comma operator with Assignment Operator:</p>\n\n<pre><code> int main()\n{\n      int i;\n      i = 1,2,3;\n      printf("i:%d\\n",i);\n      return 0;\n}\n</code></pre>\n\n<p>Ouput: i=1\n<br>\nAs we know comma operator has lower precedence than assignment.....</p>\n'}]
['https://Stackoverflow.com/questions/54142', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1541/']