TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Forgotten C: The comma operator

64 pointsby donmccalmost 14 years ago

13 comments

kqr2almost 14 years ago
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() &#62; 5) { //do something } </code></pre> versus<p><pre><code> string s; read_string(s); while(s.len() &#62; 5) { //do something read_string(s); }</code></pre>
评论 #2783410 未加载
评论 #2783950 未加载
评论 #2783752 未加载
huhtenbergalmost 14 years ago
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>
antirezalmost 14 years ago
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.
评论 #2784617 未加载
bobbyialmost 14 years ago
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>
评论 #2783271 未加载
gue5talmost 14 years ago
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.
laichzeit0almost 14 years ago
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 ;)
cronalmost 14 years ago
Cute way to reverse a string, taken from the K+R:<p><pre><code> for (i = 0, j = strlen(s)-1; i &#60; j; i++, j--) c = s[i], s[i] = s[j], s[j] = c;</code></pre>
makecheckalmost 14 years ago
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.
评论 #2784272 未加载
pyrealmost 14 years ago
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.
Zakharovalmost 14 years ago
The problem with having read_string return the length is that you have to write<p><pre><code> while (read_string(s) &#62; 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.
评论 #2784692 未加载
catch23almost 14 years ago
also one the lesser used operator in javascript too.
roinshalmost 14 years ago
The comma in the for loop is quite common, it is actually on the Indian Hill coding style and standard.
ristrettoalmost 14 years ago
Could even be used with lvalues<p><pre><code> (++k, ++k, k) += 100; </code></pre> (works with gcc 3.4)
评论 #2783612 未加载