The comma operator a compiler quirk?

I am familiar with the useful construction;
int a=1, b=2, c=3;
where the comma acts as a separator.

In another post though I saw I user using this construction
while(LT_L, false);

Ignoring the fact that the statement does not do what the OP thought I was surprised to learn that this was syntactically correct and compiles. In this situation the comma actually acts as an operator.

"The comma operator is used to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the right-most expression is considered".

So;
Code: [Select]

a = (b=3, b+2);

sets b to 3 and a to 5.

I am interested to know if anybody has made use of this operator and if there is a situation where it it is really handy to have?

I am wondering if its use in 'while' and 'for' loops is just a quirk of writing compilers i.e. allowing it makes compiler design more elegant and so an operator was born ?

I’ve seen it used in for loops, especially to initialize more than one variable:

  for (p=&string, i =0; string[i] != 0; i++) { ...

But it is generally dis-recommended. (For instance, you abc statement is pretty useless. Semicolons would be better.)
I’m not aware of the initial motivation.

It may have it's valid uses, but in general it may make the code more confusing to understand. If one would require and indexer or counter to be set for each iteration of a while loop, then it could be done either of these ways:

bool ok = true;

while (i=0, ok)
{
  //Do something
}

while (ok)
{
  i = 0;
  //Do something
}

Both loops would have "i=0" for each iteration and stop when "ok" becomes "false". The first loop would, however, set "i=0" before checking if "ok" signals end of loop causing an extra, unnecessary instruction.

The comma operator is also a sequence point.

ardly:
I am wondering if its use in 'while' and 'for' loops is just a quirk of writing compilers i.e. allowing it makes compiler design more elegant and so an operator was born ?

Nothing to do with compiler design.

This is the C and C++ language definitions - a compiler just implements the language, whatever it is.

The operator allows side-effects to be done in an expression context, which among other things is
useful for loops as you surmise.

"dis-recommended" ?

Nothing to do with compiler design.

This is the C and C++ language definitions - a compiler just implements the language, whatever it is.

The operator allows side-effects to be done in an expression context, which among other things is
useful for loops as you surmise.

I know the language definition comes first but the designers of C obviously thought long and hard to produce something that outputs efficient code. No doubt, because of limited computing power at the time, they also considered how efficiently the language itself could be compiled and reflected that in the design.

I cannot see any real need for the comma operator and it certainly allows some very obscure code to be written. That is what made me wonder if the operator was included to make compiler design easier or operation more efficient.

So far nobody has said they love the comma operator and could not live without it.

As I said, this is nothing to do with compiler design, I know, I written compilers...

The operator is to allow side-effects within an expression context, which makes a language more
expressive and concise. Many other languages have similar constructs.

MarkT:
As I said, this is nothing to do with compiler design, I know, I written compilers...

The operator is to allow side-effects within an expression context, which makes a language more
expressive and concise. Many other languages have similar constructs.

First time round you did not say you were a compiler writer. I believe you now and you have my admiration compilers are fantastic things :slight_smile:

They are just programs that perform a well-defined task, ultimately. Clever strategies
for code generation and optimization have been accrued over the years as many people
figured out how to structure the task.

"dis-recommended" ?

As in: if you write code professionally, the most common place you are likely to run into the comma operator is in the Corporate Coding Standard, which will probably say something like "Avoid using the comma operator, or you will be subject to hostile ridicule by the Code Review Team."

As I said, this is nothing to do with compiler design, I know, I written compilers...

Are you willing to assert that "compiler design", circa 1972 was similar to modern compiler design? C has been accused of being little more than a fancy assembler for the PDP11, and I'd lay petty even odds that the comma operator came about because K&R saw some convenient way (in the compiler) of generating code for certain cases... It is, for example, a "throw away the result of the experession" command, which I suspect would be a pretty useful thing to know, come "code generation" time...

For instance, your abc statement is pretty useless.

oops. I missed that this was a declaration, rather than a statement. So it's fine.
a=1, b=2, c=3;
would be mostly useless.

westfw:
oops. I missed that this was a declaration, rather than a statement. So it's fine.
a=1, b=2, c=3;
would be mostly useless.

I thought that might be the case, I forgive you.