<p>What does the <code>,</code> operator do in C?</p>

[{'answer_id': 52558, 'author': 'lillq', 'author_id': 2064, 'author_profile': 'https://Stackoverflow.com/users/2064', 'pm_score': 8, 'selected': True, 'text': '<p>The expression:</p>\n<pre><code>(expression1,  expression2)\n</code></pre>\n<p>First <code>expression1</code> is evaluated, then <code>expression2</code> is evaluated, and the value of <code>expression2</code> is returned for the whole expression.</p>\n'}, {'answer_id': 52559, 'author': 'Ben Collins', 'author_id': 3279, 'author_profile': 'https://Stackoverflow.com/users/3279', 'pm_score': 3, 'selected': False, 'text': '<p>It causes the evaluation of multiple statements, but uses only the last one as a resulting value (rvalue, I think).</p>\n\n<p>So...</p>\n\n<pre><code>int f() { return 7; }\nint g() { return 8; }\n\nint x = (printf("assigning x"), f(), g() );\n</code></pre>\n\n<p>should result in x being set to 8.</p>\n'}, {'answer_id': 52571, 'author': 'DGentry', 'author_id': 4761, 'author_profile': 'https://Stackoverflow.com/users/4761', 'pm_score': 2, 'selected': False, 'text': "<p>As earlier answers have stated it evaluates all statements but uses the last one as the value of the expression. Personally I've only found it useful in loop expressions:</p>\n\n<pre><code>for (tmp=0, i = MAX; i &gt; 0; i--)\n</code></pre>\n"}, {'answer_id': 52590, 'author': 'Owen', 'author_id': 4790, 'author_profile': 'https://Stackoverflow.com/users/4790', 'pm_score': 2, 'selected': False, 'text': "<p>The only place I've seen it being useful is when you write a funky loop where you want to do multiple things in one of the expressions (probably the init expression or loop expression. Something like:</p>\n\n<pre><code>bool arraysAreMirrored(int a1[], int a2[], size_t size)\n{\n  size_t i1, i2;\n  for(i1 = 0, i2 = size - 1; i1 &lt; size; i1++, i2--)\n  {\n    if(a1[i1] != a2[i2])\n    {\n      return false;\n    }\n  }\n\n  return true;\n}\n</code></pre>\n\n<p>Pardon me if there are any syntax errors or if I mixed in anything that's not strict C. I'm not arguing that the , operator is good form, but that's what you could use it for. In the case above I'd probably use a <code>while</code> loop instead so the multiple expressions on init and loop would be more obvious. (And I'd initialize i1 and i2 inline instead of declaring and then initializing.... blah blah blah.)</p>\n"}, {'answer_id': 52615, 'author': 'crashmstr', 'author_id': 1441, 'author_profile': 'https://Stackoverflow.com/users/1441', 'pm_score': 7, 'selected': False, 'text': "<p>I've seen used most in <code>while</code> loops:</p>\n\n<pre><code>string s;\nwhile(read_string(s), s.len() &gt; 5)\n{\n   //do something\n}\n</code></pre>\n\n<p>It will do the operation, then do a test based on a side-effect. The other way would be to do it like this:</p>\n\n<pre><code>string s;\nread_string(s);\nwhile(s.len() &gt; 5)\n{\n   //do something\n   read_string(s);\n}\n</code></pre>\n"}, {'answer_id': 57573, 'author': 'Paul Stephenson', 'author_id': 5536, 'author_profile': 'https://Stackoverflow.com/users/5536', 'pm_score': 5, 'selected': False, 'text': '<p>The comma operator combines the two expressions either side of it into one, evaluating them both in left-to-right order.  The value of the right-hand side is returned as the value of the whole expression.\n <code>(expr1, expr2)</code> is like <code>{ expr1; expr2; }</code> but you can use the result of <code>expr2</code> in a function call or assignment.</p>\n\n<p>It is often seen in <code>for</code> loops to initialise or maintain multiple variables like this:</p>\n\n<pre><code>for (low = 0, high = MAXSIZE; low &lt; high; low = newlow, high = newhigh)\n{\n    /* do something with low and high and put new values\n       in newlow and newhigh */\n}\n</code></pre>\n\n<p>Apart from this, I\'ve only used it "in anger" in one other case, when wrapping up two operations that should always go together in a macro.  We had code that copied various binary values into a byte buffer for sending on a network, and a pointer maintained where we had got up to:</p>\n\n<pre><code>unsigned char outbuff[BUFFSIZE];\nunsigned char *ptr = outbuff;\n\n*ptr++ = first_byte_value;\n*ptr++ = second_byte_value;\n\nsend_buff(outbuff, (int)(ptr - outbuff));\n</code></pre>\n\n<p>Where the values were <code>short</code>s or <code>int</code>s we did this:</p>\n\n<pre><code>*((short *)ptr)++ = short_value;\n*((int *)ptr)++ = int_value;\n</code></pre>\n\n<p>Later we read that this was not really valid C, because <code>(short *)ptr</code> is no longer an l-value and can\'t be incremented, although our compiler at the time didn\'t mind.  To fix this, we split the expression in two:</p>\n\n<pre><code>*(short *)ptr = short_value;\nptr += sizeof(short);\n</code></pre>\n\n<p>However, this approach relied on all developers remembering to put both statements in all the time.  We wanted a function where you could pass in the output pointer, the value and and the value\'s type.  This being C, not C++ with templates, we couldn\'t have a function take an arbitrary type, so we settled on a macro:</p>\n\n<pre><code>#define ASSIGN_INCR(p, val, type)  ((*((type) *)(p) = (val)), (p) += sizeof(type))\n</code></pre>\n\n<p>By using the comma operator we were able to use this in expressions or as statements as we wished:</p>\n\n<pre><code>if (need_to_output_short)\n    ASSIGN_INCR(ptr, short_value, short);\n\nlatest_pos = ASSIGN_INCR(ptr, int_value, int);\n\nsend_buff(outbuff, (int)(ASSIGN_INCR(ptr, last_value, int) - outbuff));\n</code></pre>\n\n<p>I\'m not suggesting any of these examples are good style!  Indeed, I seem to remember Steve McConnell\'s <em>Code Complete</em> advising against even using comma operators in a <code>for</code> loop: for readability and maintainability, the loop should be controlled by only one variable, and the expressions in the <code>for</code> line itself should only contain loop-control code, not other extra bits of initialisation or loop maintenance.</p>\n'}, {'answer_id': 18444099, 'author': 'Shafik Yaghmour', 'author_id': 1708801, 'author_profile': 'https://Stackoverflow.com/users/1708801', 'pm_score': 6, 'selected': False, 'text': '<p>The <a href="https://en.wikipedia.org/wiki/Comma_operator" rel="noreferrer">comma operator</a> will evaluate the left operand, discard the result and then evaluate the right operand and that will be the result. The <em>idiomatic</em> use as noted in the link is when initializing the variables used in a <code>for</code> loop, and it gives the following example:</p>\n\n<pre><code>void rev(char *s, size_t len)\n{\n  char *first;\n  for ( first = s, s += len - 1; s &gt;= first; --s)\n      /*^^^^^^^^^^^^^^^^^^^^^^^*/ \n      putchar(*s);\n}\n</code></pre>\n\n<p>Otherwise there are not many <em>great</em> uses of the <em>comma operator</em>, although it is easy to abuse to generate code that is hard to read and maintain.</p>\n\n<p>From the <a href="http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf" rel="noreferrer">draft C99 standard</a> the grammar is as follows:</p>\n\n<pre><code>expression:\n  assignment-expression\n  expression , assignment-expression\n</code></pre>\n\n<p>and <em>paragraph 2</em> says:</p>\n\n<blockquote>\n  <p>The <strong>left operand of a comma operator is evaluated as a void expression;</strong> there is a sequence point after its evaluation. Then the <strong>right operand is evaluated; the result has its type and value.</strong> <sup>97)</sup> If an attempt is made to modify the result of a comma operator or to access it after the next sequence point, the behavior is undefined.</p>\n</blockquote>\n\n<p><em>Footnote 97</em> says:</p>\n\n<blockquote>\n  <p>A comma operator does <strong>not yield an lvalue</strong>.</p>\n</blockquote>\n\n<p>which means you can not assign to the result of the <em>comma operator</em>.</p>\n\n<p>It is important to note that the comma operator has the <a href="https://en.cppreference.com/w/c/language/operator_precedence" rel="noreferrer">lowest precedence</a> and therefore there are cases where using <code>()</code> can make a big difference, for example:</p>\n\n<pre><code>#include &lt;stdio.h&gt;\n\nint main()\n{\n    int x, y ;\n\n    x = 1, 2 ;\n    y = (3,4) ;\n\n    printf( "%d %d\\n", x, y ) ;\n}\n</code></pre>\n\n<p>will have the following output:</p>\n\n<pre><code>1 4\n</code></pre>\n'}, {'answer_id': 54038455, 'author': 'ViNi89', 'author_id': 5770705, 'author_profile': 'https://Stackoverflow.com/users/5770705', 'pm_score': -1, 'selected': False, 'text': '<p>I\'m reviving this simply to address questions from @Rajesh and @JeffMercado which i think are very important since this is one of the top search engine hits.</p>\n\n<p>Take the following snippet of code for example </p>\n\n<pre><code>int i = (5,4,3,2,1);\nint j;\nj = 5,4,3,2,1;\nprintf("%d %d\\n", i , j);\n</code></pre>\n\n<p>It will print</p>\n\n<pre><code>1 5\n</code></pre>\n\n<p>The <code>i</code> case is handled as explained by most answers. All expressions are evaluated in left-to-right order but only the last one is assigned to <code>i</code>. The result of the <code>(</code> <em>expression</em> )<code>is</code>1`. </p>\n\n<p>The <code>j</code> case follows different precedence rules since <code>,</code> has the lowest operator precedence. Because of those rules, the compiler sees <em>assignment-expression, constant, constant ...</em>. The expressions are again evaluated in left-to-right order and their side-effects stay visible, therefore, <code>j</code> is <code>5</code> as a result of <code>j = 5</code>.</p>\n\n<p>Interstingly, <code>int j = 5,4,3,2,1;</code> is not allowed by the language spec. An <em>initializer</em> expects an <em>assignment-expression</em> so a direct <code>,</code> operator is not allowed.</p>\n\n<p>Hope this helps.</p>\n'}]
['https://Stackoverflow.com/questions/52550', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2064/']