6 ms·
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 use
by piadodjanho 7y ago
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.
- jakear 7y agoThe comma operator isn’t very obscure at all. Even JS has it.
- kbenson 7y agoIt'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.
- jakear 7y agoRight, that’s also the case for comma in JS.
- piadodjanho 7y agoI reckon that most language with grammar inspired in C describes the comma operator.
- kbenson 7y agoSo, 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.
- ndesaulniers 7y agoThe 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.
- sfoley 7y agoThe comma character as a token in variable is decalararions is not the same thing as the comma operator.
- piadodjanho 7y agoYou are correct. My mistake.
- Asooka 7y agoAlso, 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.
- piadodjanho 7y agoSo that's why it return the value of last expression!