comma operator

Karl Botts kdb at chinet.chi.il.us
Thu Aug 10 14:53:54 AEST 1989


>> Maybe I'm mistaken, but I'm sure that all the documentation I've read warns
>> that the *comma* operator ----does not----- guarantee evaluation in any order.
>> Specifically, the Microsoft 5.0 manual mentions this.
>> 
>If this is true for MSC 5.0, they fixed it in MSC 5.1.

It was never true, certainly not for any Microsoft compiler since 4.0, and
not for any compiler I ever heard of (there have bben some that did not
support the comma operator at all).  

I think there are legitimate places for the comma operator outside macro
expnsions and loop control statements.  YOu just have to remember that it
has the lowest precedence of any operator, and thus must be protected with
perens.

Using the comma operator can emphasize that several statements are
logically atomic -- that together, they are doing something which may be
though of as asingle operation.  for instance:

i += x, j -=x;
if ( i >= j )
	...

I grant that the semi-colon can always be used in these cases, but I see no
harm in the comma operator.

I have also written code something like this:

linked_list_node_t *p;
if ( p && ( p = p->next, p ) )
	/* something */


No doubt there are other ways to do it, but I don't see this as a sin.



More information about the Comp.lang.c mailing list