Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

A more obscure feature is the uncommon usage of comma operator. We often use the comma operator in variable declaration and in for loops. But it can also be used in any expression.

For instance, the next line has a valid C construct:

   return a, b, c;
This is particularly useful for setting variable when retuning after an error.

   if (ret = io(x))
       return errno = 10, -1;
The possibilities are endless. Another example:

    if (x = 1, y = 2, x < 3)
     ...
But the comma operator really shines when used in conjunction with macros.


The comma operatory leads to bad bad bugs (especially when mixed with misleading indentation) and if its use was punishable I wouldn't even be sad.


The comma character as a token in variable is decalararions is not the same thing as the comma operator.


You are correct. My mistake.


Also, the comma operator forces a left to right evaluation order.

Surprisingly, you can override it in C++. I haven't seen anyone do it, but you can. If you find a good, productive override for the comma operator, please post about it.


So that's why it return the value of last expression!


The comma operator isn’t very obscure at all. Even JS has it.


It's not the comma operator that is being noted as obscure, but particular usage of it, such as complex return statements that set a value and return another.


Right, that’s also the case for comma in JS.


So, are you making the case that the examples present are common in JS as well? Because the whole point of the comment was in the uncommon usage of comma operator, as stated in the first sentence.

And to be clear, even if it is common in JS, that still doesn't reduce the usefulness of the original comment, because we aren't talking about JS, we're talking about C, and the commonness of the features notes in the C ecosystem. There are plenty of obscure oft-ignored features on one language that are common in another. For example, taking someone's interesting C macro that allows some level of equivalence to functional map and apply and saying "that isn't very obscure, even lisp has that" is missing the point.


I reckon that most language with grammar inspired in C describes the comma operator.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: