Question c faq

Discussion in 'C Programming' started by Emilio, May 14, 2006.

  1. Emilio

    Emilio Guest

    Hi,
    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 tried it in a program and it that prints out

    DEBUG: "main.c", line 10: i is 3

    giving i the value 3. In this case it gives the same output if the
    comma was changed for a ;.

    I don't understand what it is used for how how it works.

    TIA
    Emilio, May 14, 2006
    #1
    1. Advertising

  2. Emilio

    Eric Sosman Guest

    Emilio wrote:
    > Hi,
    > 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 tried it in a program and it that prints out
    >
    > DEBUG: "main.c", line 10: i is 3
    >
    > giving i the value 3. In this case it gives the same output if the
    > comma was changed for a ;.
    >
    > I don't understand what it is used for how how it works.


    Try it both ways (with comma and with semicolon)
    in a context like

    if (i < 0)
    printf("DEBUG: \"%s\", line %d: ",
    __FILE__,__LINE__), printf("i is %d", i);

    .... and be sure to try both negative and positive values
    of `i'. Ask again if it's still confusing.

    --
    Eric Sosman
    lid
    Eric Sosman, May 14, 2006
    #2
    1. Advertising

  3. Emilio

    Emilio Guest

    what I wanted to know is why use
    printf("DEBUG: \"%s\", line %d: ", __FILE__,__LINE__),
    printf("i is %d", i);

    and not
    printf("DEBUG: \"%s\", line %d: ", __FILE__,__LINE__);
    printf("i is %d", i);

    I've been searching and it's the comma operator that bugs me. I don'
    see why to use it with printf expressions in macros( this example is
    from the expanded macro from the c faq.

    Btw i value does not chand the printed output
    Emilio, May 14, 2006
    #3
  4. Emilio

    Emilio Guest

    Right, I think I've got it.
    The porpoises of the macro in the faq was to have several expression
    executed as one. One way to do it would be

    #define onemacro(p) do { print1(...); print2(...); } while(0)
    print1 and print2 are execued in the same block

    which is the same as

    #define onemacro(p) print1(...), print2(...);
    Emilio, May 14, 2006
    #4
  5. Emilio

    Emilio Guest

    Actually is in quesion 10.4 of the faq :)
    I missed it the first time :)
    Emilio, May 14, 2006
    #5
  6. Emilio

    CBFalconer Guest

    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);

    --
    "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/>
    CBFalconer, May 14, 2006
    #6
  7. "Emilio" <> writes:
    > 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 tried it in a program and it that prints out
    >
    > DEBUG: "main.c", line 10: i is 3
    >
    > giving i the value 3. In this case it gives the same output if the
    > comma was changed for a ;.
    >
    > I don't understand what it is used for how how it works.


    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).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, May 14, 2006
    #7
  8. "Emilio" <> writes:
    > Actually is in quesion 10.4 of the faq :)
    > I missed it the first time :)


    Please read <http://cfaj.freeshell.org/google/>.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, May 14, 2006
    #8
  9. CBFalconer <> writes:
    > 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);


    The code in the FAQ shouldn't lead to undefined behavior if used
    properly.

    Here's the macro definition from the FAQ:

    #define DEBUG printf("DEBUG: \"%s\", line %d: ", \
    __FILE__,__LINE__),printf

    Then this:

    DEBUG("i is %d", i);

    expands to:

    printf("DEBUG: \"%s\", line %d: ",
    __FILE__,__LINE__),printf("i is %d", i);

    It's tricksy, but it works.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, May 14, 2006
    #9
  10. Emilio

    Eric Sosman Guest

    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?)

    --
    Eric Sosman
    lid
    Eric Sosman, May 14, 2006
    #10
  11. Emilio

    Ben C Guest

    On 2006-05-14, Eric Sosman <> 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?
    Ben C, May 14, 2006
    #11
  12. Emilio

    Malcolm Guest

    "Keith Thompson" <> 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
    Malcolm, May 14, 2006
    #12
  13. Emilio

    Eric Sosman Guest

    Ben C wrote:
    > On 2006-05-14, Eric Sosman <> 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
    lid
    Eric Sosman, May 14, 2006
    #13
  14. Emilio

    Ben C Guest

    On 2006-05-14, Ben C <> wrote:
    > On 2006-05-14, Eric Sosman <> 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.
    Ben C, May 14, 2006
    #14
  15. Malcolm <> 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.
    Christopher Benson-Manica, May 15, 2006
    #15
  16. Emilio

    CBFalconer Guest

    Eric Sosman wrote:
    >
    > Ben C wrote:
    > > On 2006-05-14, Eric Sosman <> 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/>
    CBFalconer, May 15, 2006
    #16
  17. Emilio

    CBFalconer Guest

    Christopher Benson-Manica wrote:
    > Malcolm <> 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/>
    CBFalconer, May 15, 2006
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philip Lawatsch
    Replies:
    9
    Views:
    765
    tom_usenet
    Jul 9, 2003
  2. =?ISO-8859-15?Q?Juli=E1n?= Albo

    FAQ or not FAQ?

    =?ISO-8859-15?Q?Juli=E1n?= Albo, Jan 8, 2007, in forum: C++
    Replies:
    28
    Views:
    837
    Grizlyk
    Jan 15, 2007
  3. Josef 'Jupp' Schugt
    Replies:
    0
    Views:
    184
    Josef 'Jupp' Schugt
    Sep 22, 2003
  4. FAQ server

    FAQ - How do I direct someone to this FAQ?

    FAQ server, Aug 4, 2006, in forum: Javascript
    Replies:
    1
    Views:
    145
    Dr John Stockton
    Aug 4, 2006
  5. Peter Michaux

    FAQ/FAQ notes site makeover

    Peter Michaux, Nov 22, 2006, in forum: Javascript
    Replies:
    22
    Views:
    330
    Randy Webb
    Nov 27, 2006
Loading...

Share This Page