This is probably one of the better use cases for the comma operator in C.<p><a href="http://stackoverflow.com/questions/52550/what-does-the-operator-do-in-c" rel="nofollow">http://stackoverflow.com/questions/52550/what-does-the-opera...</a><p><pre><code> string s;
while(read_string(s), s.len() > 5)
{
//do something
}
</code></pre>
versus<p><pre><code> string s;
read_string(s);
while(s.len() > 5)
{
//do something
read_string(s);
}</code></pre>
Comma is quite useful for compacting cleanup code in abnormal function exits. E.g. from this:<p><pre><code> if (...)
{
cleanup();
return -2;
}
</code></pre>
to this<p><pre><code> if (...)
return cleanup(), -2;
</code></pre>
Linux kernel uses this pattern in several places. However the best comma use is, of course, for messing your enemies' code:<p><pre><code> foo,();</code></pre>
The comma operator is pretty useful in macros:<p><pre><code> #define something(...) (printf(... some debug message ...),real_something_expression)
foo = something(...)
</code></pre>
This way you can create macros returning values and printing debug messages at the same time.
C++ allows overloading the comma operator. This can be (ab)used to make the following syntax work for, e.g., initializing a vectorish type:<p><pre><code> MyType obj = 1, 2, 3;
</code></pre>
Due to the precedence of operators, this is actually equivalent to<p><pre><code> ((MyType obj = 1), 2), 3;
</code></pre>
so as long as the overloaded assignment operator returns an object that the comma operator mutates and returns, this can be made the have effect of initializing the declared object to a "list".<p>This trick is used by Boost Spirit to allow things like (example from <a href="http://stackoverflow.com/questions/54142/c-comma-operator" rel="nofollow">http://stackoverflow.com/questions/54142/c-comma-operator</a>)<p><pre><code> keywords = "and", "or", "not", "xor";</code></pre>
It's also occasionally useful in the construction of #define macros, where readability is often sacrificed for proper semantics regarding side effects and part-of-speech.
For those who want to see a serious use of this operator, see <a href="http://code.google.com/p/cii/source/browse/trunk/include/except.h" rel="nofollow">http://code.google.com/p/cii/source/browse/trunk/include/exc...</a> line 35. This from an exception (try, catch, except macros) library for the C language. It is from a book called "C interfaces and implementations", probably one of the best books on good C programming I have ever (and still) read.<p>It's special in the fact that it's written by Hanson (who also co-wrote the LCC compiler, you might have seen ID Software use this compiler) and the book uses Knuth's Literary Programming technique for the source code in the book ;)
Cute way to reverse a string, taken from the K+R:<p><pre><code> for (i = 0, j = strlen(s)-1; i < j; i++, j--)
c = s[i], s[i] = s[j], s[j] = c;</code></pre>
I find the comma useful in deletion scenarios. For example, rather than just "free(x)" or "delete x" (after which the variable is undefined and can't be used anyway), I like to use the comma to add a NULL assignment. So, "free(x), x = NULL". It doesn't really need to be that way, but it's nice for avoiding random behavior in case the variable is referenced later; and the chain on one line makes it unlikely that the 2nd part will be forgotten.
In Perl:<p><pre><code> my ($a) = (8,9) || (20,21);
print "\$a = $a\n";
</code></pre>
Gives:<p><pre><code> $a = 9
</code></pre>
Due to the comma operator.
The problem with having read_string return the length is that you have to write<p><pre><code> while (read_string(s) > 5) // loop until the string length is greater than 5
</code></pre>
because it's not intuitive that read_string will return the length of the string. You don't save anything this way.