Mega Search
23.2 Million


Sign Up

Make a donation  
COMMA-operator or separator  
News Group: comp.lang.c

I have doubts on the following statments
========================================

1. Sometimes comma operators are used as a separator and sometimes as a operator
2. Comma doesn't introduce a sequence point b/w function call arguments but it 
   does when we give a simple statment like -  i=(a,b,c);

Do we have any generalized rules for both of the statments?

Vote for best question.
Score: 0  # Vote:  0
Date Posted: 27-Aug-2014, at 8:13 AM EST
From: SAHIL MAHLA
 
Re: COMMA-operator or separator  
News Group: comp.lang.c
James Dow Allen  writes:

> The comma operator is often used in for statements:
>     for (ix = 0, p = Ibuff; *p; ix++, p++)
>           whatever(...);
> Here the sequence-point feature is unnecessary;
> but it is still a common operator. Its use here
> is easily avoided, but why bother?

Every now and then it can be used to fix a broken API:

  while (get_data(&somewhere, &errors), !errors) {
       ...
  }


-- 
Ben.

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 28-Aug-2014, at 7:07 PM EST
From: Ben Bacarisse
 
Re: COMMA-operator or separator  
News Group: comp.lang.c
The comma operator is often used in for statements:
    for (ix = 0, p = Ibuff; *p; ix++, p++)
          whatever(...);
Here the sequence-point feature is unnecessary;
but it is still a common operator. Its use here
is easily avoided, but why bother?

Nice macros can be fashioned using comma operators
which would otherwise be difficult or impossible.

BTW, there is a simple syntactical check which,
I think, will inform you whether or not a given
comma is the "comma operator":  If
    ... A, B ...    or    (A, B)
have semantic effects *identical* to, respectively,
    ... (A, B)     or    ((A, B))
then the commas ARE the comma operator.  If not, they're NOT.
Any exceptions?

James Dow Allen


Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 28-Aug-2014, at 8:18 AM EST
From: James Dow Allen
 
Re: COMMA-operator or separator  
News Group: comp.lang.c
On 8/28/14 7:43 AM, David Brown wrote:
> On 27/08/14 18:08, Malcolm McLean wrote:
>> On Wednesday, August 27, 2014 4:33:01 PM UTC+1, Richard Heathfield wrote:
>>>
>>> Use a comma operator when you need a comma operator. Use a comma separator
>>> when you need a comma separator.
>>>
>>> I'm sorry. I know this reply sounds banal.
>>>
>> Sounds banal. But accepted wisdom is never ever use a comma operator,
>> even when it's the neatest solution to the problem. Always find a way
>> or rewriting code without it.
>>
>
> You are starting a fight here...
>
> I fully agree with you that the comma operator should never be used -
> but I don't think it is "accepted wisdom" by many.  It is accepted
> wisdom by all coding standards that I have seen where there is an
> emphasis on writing clear, safe and reliable code (I've just done a
> quick check of Misra, JSF, PRQA).  But as we already know, C programmers
> (in c.l.c. and elsewhere) often write code that they call "idiomatic C",
> while safe programming experts prefer the term "idiotic C".
>
> Misra allows the comma operator in for-loops, as described by James, but
> JSF does not allow that either.

Observation 1: Those who represent themselves as experts in the field of 
“Conventional Wisdom” seem to universally lack expertise in others.

Observation 2: Programmers who blame their errors on the programming 
language they’re using probably shouldn’t be.

Observation 3: Dartmouth BASIC is still available.

-- 
Morris Dovey
http://www.iedu.com/Solar/
http://www.facebook.com/MorrisDovey

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 28-Aug-2014, at 10:04 AM EST
From: Morris Dovey
 
Re: COMMA-operator or separator  
News Group: comp.lang.c
On 27/08/14 18:08, Malcolm McLean wrote:
> On Wednesday, August 27, 2014 4:33:01 PM UTC+1, Richard Heathfield wrote:
>>
>> Use a comma operator when you need a comma operator. Use a comma separator 
>> when you need a comma separator.
>>
>> I'm sorry. I know this reply sounds banal.
>>
> Sounds banal. But accepted wisdom is never ever use a comma operator,
> even when it's the neatest solution to the problem. Always find a way
> or rewriting code without it.
> 

You are starting a fight here...

I fully agree with you that the comma operator should never be used -
but I don't think it is "accepted wisdom" by many.  It is accepted
wisdom by all coding standards that I have seen where there is an
emphasis on writing clear, safe and reliable code (I've just done a
quick check of Misra, JSF, PRQA).  But as we already know, C programmers
(in c.l.c. and elsewhere) often write code that they call "idiomatic C",
while safe programming experts prefer the term "idiotic C".

Misra allows the comma operator in for-loops, as described by James, but
JSF does not allow that either.


Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 28-Aug-2014, at 2:43 PM EST
From: David Brown
 
Re: COMMA-operator or separator  
News Group: comp.lang.c
glen herrmannsfeldt  wrote:

> Just a note, Java doesn't have a comma operator, but the for syntax
> includes commas for the first and third expression of the for statement.

That's like saying English doesn't have a possessive, but the personal
pronoun includes a genitive which functions as one.
English has a possessive and Java has a comma operator - just not one
that can be used as generally as C's.

Richard

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 28-Aug-2014, at 11:27 AM EST
From: Richard Bos