Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question c faq

Reply
Thread Tools

Question c faq

 
 
Ben C
Guest
Posts: n/a
 
      05-14-2006
On 2006-05-14, Eric Sosman <(E-Mail Removed)> wrote:
> CBFalconer wrote:
>> Emilio wrote:
>>
>>>I was reading the faqs from c-faq.com and on the C Preprocesor
>>>Section in question 10.27 there is a piece of code:
>>>
>>>printf("DEBUG: \"%s\", line %d: ",
>>> __FILE__,__LINE__),printf("i is %d", i);
>>>
>>>That I quite don't understand. Whats the comma for?. I thought
>>>it should be changed for a ;

>>
>>
>> I assume you are worrying about the comma in "),printf". It is
>> actually very poor code leading to undefined behaviour, IMNSHO. A
>> clearer (and more likely correct) version might be:
>>
>> printf("DEBUG: \"%s\", line %d: i is %d",
>> __FILE__, __LINE__, i);
>>

>
> Could you explain your NSHO? What is it in the original
> that might provoke undefined behavior? (Let's assume that the
> fragment appears in an executable context, with an appropriate
> declaration of `i' in scope, with <stdio.h> included, and that
> the program will eventually output a newline -- what is it about
> the fragment itself that troubles you?)


I wonder if in the former version the second printf expression could be
evaluated before the first one?
 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      05-14-2006
"Keith Thompson" <(E-Mail Removed)> wrote
> It's a comma operator. It evaluates its left operand (and discards
> the result), then it evaluates the right operand and yields its
> result.
>
> It can be a little confusing because the comma is used for other
> things as well (such as separating function arguments).
>

It is also the first time I've seen one used, since as long as I can
remember.
--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm



 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      05-14-2006
Ben C wrote:
> On 2006-05-14, Eric Sosman <(E-Mail Removed)> wrote:
>
>>CBFalconer wrote:
>>
>>>Emilio wrote:
>>>
>>>
>>>>I was reading the faqs from c-faq.com and on the C Preprocesor
>>>>Section in question 10.27 there is a piece of code:
>>>>
>>>>printf("DEBUG: \"%s\", line %d: ",
>>>> __FILE__,__LINE__),printf("i is %d", i);
>>>>
>>>>That I quite don't understand. Whats the comma for?. I thought
>>>>it should be changed for a ;
>>>
>>>
>>>I assume you are worrying about the comma in "),printf". It is
>>>actually very poor code leading to undefined behaviour, IMNSHO. A
>>>clearer (and more likely correct) version might be:
>>>
>>> printf("DEBUG: \"%s\", line %d: i is %d",
>>> __FILE__, __LINE__, i);
>>>

>>
>> Could you explain your NSHO? What is it in the original
>>that might provoke undefined behavior? (Let's assume that the
>>fragment appears in an executable context, with an appropriate
>>declaration of `i' in scope, with <stdio.h> included, and that
>>the program will eventually output a newline -- what is it about
>>the fragment itself that troubles you?)

>
>
> I wonder if in the former version the second printf expression could be
> evaluated before the first one?


No, by the properties of the comma operator. The
left operand is guaranteed to be evaluated first, and
there is a sequence point before starting to evaluate
the right operand. That's why I'm perplexed by CBF's
uneasiness.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Ben C
Guest
Posts: n/a
 
      05-14-2006
On 2006-05-14, Ben C <(E-Mail Removed)> wrote:
> On 2006-05-14, Eric Sosman <(E-Mail Removed)> wrote:
>> CBFalconer wrote:
>>> Emilio wrote:
>>>
>>>>I was reading the faqs from c-faq.com and on the C Preprocesor
>>>>Section in question 10.27 there is a piece of code:
>>>>
>>>>printf("DEBUG: \"%s\", line %d: ",
>>>> __FILE__,__LINE__),printf("i is %d", i);
>>>>
>>>>That I quite don't understand. Whats the comma for?. I thought
>>>>it should be changed for a ;
>>>
>>>
>>> I assume you are worrying about the comma in "),printf". It is
>>> actually very poor code leading to undefined behaviour, IMNSHO. A
>>> clearer (and more likely correct) version might be:
>>>
>>> printf("DEBUG: \"%s\", line %d: i is %d",
>>> __FILE__, __LINE__, i);
>>>

>>
>> Could you explain your NSHO? What is it in the original
>> that might provoke undefined behavior? (Let's assume that the
>> fragment appears in an executable context, with an appropriate
>> declaration of `i' in scope, with <stdio.h> included, and that
>> the program will eventually output a newline -- what is it about
>> the fragment itself that troubles you?)

>
> I wonder if in the former version the second printf expression could be
> evaluated before the first one?


No it can't be, I just checked.
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      05-15-2006
Malcolm <(E-Mail Removed)> wrote:

> It is also the first time I've seen one used, since as long as I can
> remember.


I haven't looked at the FAQ to see what the rationale for using it
was, but I can say that the only time I've used the comma operator was
to hack around a compiler bug.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      05-15-2006
Eric Sosman wrote:
>
> Ben C wrote:
> > On 2006-05-14, Eric Sosman <(E-Mail Removed)> wrote:
> >
> >>CBFalconer wrote:
> >>
> >>>Emilio wrote:
> >>>
> >>>
> >>>>I was reading the faqs from c-faq.com and on the C Preprocesor
> >>>>Section in question 10.27 there is a piece of code:
> >>>>
> >>>>printf("DEBUG: \"%s\", line %d: ",
> >>>> __FILE__,__LINE__),printf("i is %d", i);
> >>>>
> >>>>That I quite don't understand. Whats the comma for?. I thought
> >>>>it should be changed for a ;
> >>>
> >>>
> >>>I assume you are worrying about the comma in "),printf". It is
> >>>actually very poor code leading to undefined behaviour, IMNSHO. A
> >>>clearer (and more likely correct) version might be:
> >>>
> >>> printf("DEBUG: \"%s\", line %d: i is %d",
> >>> __FILE__, __LINE__, i);
> >>>
> >>
> >> Could you explain your NSHO? What is it in the original
> >>that might provoke undefined behavior? (Let's assume that the
> >>fragment appears in an executable context, with an appropriate
> >>declaration of `i' in scope, with <stdio.h> included, and that
> >>the program will eventually output a newline -- what is it about
> >>the fragment itself that troubles you?)

> >
> >
> > I wonder if in the former version the second printf expression could be
> > evaluated before the first one?

>
> No, by the properties of the comma operator. The
> left operand is guaranteed to be evaluated first, and
> there is a sequence point before starting to evaluate
> the right operand. That's why I'm perplexed by CBF's
> uneasiness.


I misread it, and had the 2nd printf as an argument to the DEBUG.
I still don't like it.

IMNSHO = in my not so humble opinion.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      05-15-2006
Christopher Benson-Manica wrote:
> Malcolm <(E-Mail Removed)> wrote:
>
>> It is also the first time I've seen one used, since as long as
>> I can remember.

>
> I haven't looked at the FAQ to see what the rationale for using
> it was, but I can say that the only time I've used the comma
> operator was to hack around a compiler bug.


A common use, at least for me, is in for initialization and
looping:

for (x = 0, y = 1, z = 2; x < MAX; x++, y--) {
/* stuff using x, y, z */
}

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ or not FAQ? =?ISO-8859-15?Q?Juli=E1n?= Albo C++ 28 01-15-2007 04:33 AM
FAQ/FAQ notes site makeover Peter Michaux Javascript 22 11-27-2006 01:55 AM
FAQ - How do I direct someone to this FAQ? FAQ server Javascript 1 08-04-2006 10:13 PM
[de] Update of FAQ in German/FAQ auf Deutsch ueberarbeitet Josef 'Jupp' Schugt Ruby 0 09-22-2003 08:56 PM
Question about Memory Pool Example in C++ Faq Philip Lawatsch C++ 9 07-09-2003 04:44 PM



Advertisments