how the macro works

Discussion in 'C Programming' started by George2, Dec 12, 2007.

  1. George2

    George2 Guest

    Hello everyone,


    I can not understand how the following code works and assign 100 to
    counter variable?

    Code:
    void Foo (int* input)
    {
    	*input = 100;
    
    	return;
    }
    
    #define GETFOO  (Foo( &counter ), counter)
    
    int counter;
    
    int main()
    {
    	GETFOO;
    	return 0;
    }
    

    thanks in advance,
    George
    George2, Dec 12, 2007
    #1
    1. Advertising

  2. George2 said:

    > Hello everyone,
    >
    >
    > I can not understand how the following code works and assign 100 to
    > counter variable?
    >
    > void Foo (int* input)
    > {
    > *input = 100;
    >
    > return;
    > }
    >
    > #define GETFOO (Foo( &counter ), counter)
    >
    > int counter;
    >
    > int main()
    > {
    > GETFOO;
    > return 0;
    > }


    After macro replacements have occurred, your program becomes:

    void Foo (int* input)
    {
    *input = 100;

    return;
    }


    int counter;

    int main()
    {
    (Foo( &counter ), counter);
    return 0;
    }

    Now that you see it like that, does it strike you as being a bit weird?


    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
    Richard Heathfield, Dec 12, 2007
    #2
    1. Advertising

  3. George2 <> wrote in news:c0f8e56c-9fb3-40d6-a504-
    :

    > I can not understand how the following code works and assign 100 to
    > counter variable?



    My guess is you don't know how the comma operator works.

    It takes two operands, a left one and a right one.

    It evaluates the left operand, then evaluates the right operand, and the
    resulting expression is equal to the right operand. (I can't remember if
    you can use it as an L-value, but my guess would be no).

    If you have:

    y = Func(), 5;


    then it's as if you wrote:

    Func();

    y = 5;

    That is: "Func()" gets evaluated, then "5" gets evaluated, and the
    resultant expression is "5".


    --
    Tomás Ó hÉilidhe
    Tomás Ó hÉilidhe, Dec 12, 2007
    #3
  4. George2

    Chris Dollin Guest

    Tomás Ó hÉilidhe wrote:

    (Those are funny characters in the name.)

    > George2 <> wrote in news:c0f8e56c-9fb3-40d6-a504-
    > :
    >
    >> I can not understand how the following code works and assign 100 to
    >> counter variable?

    >
    > My guess is you don't know how the comma operator works.
    >
    > It takes two operands, a left one and a right one.
    >
    > It evaluates the left operand, then evaluates the right operand, and the
    > resulting expression is equal to the right operand. (I can't remember if
    > you can use it as an L-value, but my guess would be no).


    So far, so good, but:

    > If you have:
    >
    > y = Func(), 5;
    >
    > then it's as if you wrote:
    >
    > Func();
    >
    > y = 5;


    No; it's as though you'd written

    y = Func(); 5;

    Comma is the least binding operator (when it's an operator at all).

    Example code:

    #include <stdio.h>

    int func(void)
    { return 17; }

    int main(void)
    {
    int y;
    y = func(), 5;
    fprintf( stderr, "y == %d\n", y );
    return 0;
    }

    Output:

    y == 17

    --
    Chris "an operator of unspecified precedence" Dollin

    Hewlett-Packard Limited Cain Road, Bracknell, registered no:
    registered office: Berks RG12 1HN 690597 England
    Chris Dollin, Dec 12, 2007
    #4
  5. Chris Dollin <> wrote in
    news:fjomsl$1tk$:

    > No; it's as though you'd written
    >
    > y = Func(); 5;
    >
    > Comma is the least binding operator (when it's an operator at all).



    Wups a daisy, I should laminate an operator precedence table and blue-tack
    it to my laptop :-D.

    --
    Tomás Ó hÉilidhe
    Tomás Ó hÉilidhe, Dec 12, 2007
    #5
  6. George2

    Mark Bluemel Guest

    Tomás Ó hÉilidhe wrote:
    > Chris Dollin <> wrote in
    > news:fjomsl$1tk$:
    >
    >> No; it's as though you'd written
    >>
    >> y = Func(); 5;
    >>
    >> Comma is the least binding operator (when it's an operator at all).

    >
    >
    > Wups a daisy, I should laminate an operator precedence table and blue-tack
    > it to my laptop :-D.
    >

    Or test your assertions with a program...
    Mark Bluemel, Dec 12, 2007
    #6
  7. George2

    hakuna Guest

    > Example code:
    >
    > #include <stdio.h>
    >
    > int func(void)
    > { return 17; }
    >
    > int main(void)
    > {
    > int y;
    > y = func(), 5;
    > fprintf( stderr, "y == %d\n", y );
    > return 0;
    > }
    >
    > Output:
    >
    > y == 17


    get the right operand like this:
    y = (func(), 5);

    the Output is: y==5
    hakuna, Dec 12, 2007
    #7
  8. Chris Dollin wrote:
    > Tom�s � h�ilidhe wrote:
    >
    > (Those are funny characters in the name.)


    Not as funny as they appear to me:
    "Tom��������������������������������" wrote:
    Philip Potter, Dec 12, 2007
    #8
  9. Philip Potter <> wrote in news:fjp0c2$v2j$:

    > Chris Dollin wrote:
    >> Tom�s � h�ilidhe wrote:
    >>
    >> (Those are funny characters in the name.)

    >
    > Not as funny as they appear to me:
    > "Tom����������������������ï
    > ¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½" wrote:




    Do you not use UTF-8 in your newsreader?

    --
    Tomás Ó hÉilidhe
    Tomás Ó hÉilidhe, Dec 12, 2007
    #9
  10. "Tom��������������������������������" wrote:
    > Philip Potter <> wrote in news:fjp0c2$v2j$:
    >
    >> Chris Dollin wrote:
    >>> Tom�s � h�ilidhe wrote:
    >>>
    >>> (Those are funny characters in the name.)

    >> Not as funny as they appear to me:
    >> "Tom����������������������ï
    >> ¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½" wrote:

    >
    > Do you not use UTF-8 in your newsreader?


    The important thing is that you do not set a character encoding in your
    headers. For example:

    Content-Type: text/plain; charset=utf-8

    If you don't provide this information, my newsreader cannot magically
    know which character set you're using; and you have no reason to expect
    it to default to UTF-8 because I might meet someone else who expects it
    to default to ISO-8859-1.

    As a matter of fact I do use UTF-8 as default. If I change it to
    ISO-8859-1 then I get "Tomás Ó hÉilidhe wrote:" which looks more likely.
    So it seems you aren't using UTF-8 at all, but one of the ISO-8859 family.

    Unfortunately, I don't have any advice for you on how to rectify the
    situation because I use a different newsreader. Nevertheless, I wish you
    the best of luck fixing it.

    Phil
    Philip Potter, Dec 12, 2007
    #10
  11. Philip Potter <> wrote in news:fjp525$dj5$:

    > The important thing is that you do not set a character encoding in
    > your headers. For example:
    >
    > Content-Type: text/plain; charset=utf-8
    >
    > If you don't provide this information, my newsreader cannot magically
    > know which character set you're using; and you have no reason to
    > expect it to default to UTF-8 because I might meet someone else who
    > expects it to default to ISO-8859-1.
    >
    > As a matter of fact I do use UTF-8 as default. If I change it to
    > ISO-8859-1 then I get "Tomás Ó hÉilidhe wrote:" which looks more
    > likely. So it seems you aren't using UTF-8 at all, but one of the
    > ISO-8859 family.
    >
    > Unfortunately, I don't have any advice for you on how to rectify the
    > situation because I use a different newsreader. Nevertheless, I wish
    > you the best of luck fixing it.



    I started out using a Linux newsreader called Pan, but it turned out to be
    crap.

    At the moment I'm using a Windows newsreader called XNews, which is
    supposedly the best newsreader ever (or so say the people who recommended
    it to me). I wouldn't have thought that XNews got it wrong when it came to
    character encoding. . . ?

    --
    Tomás Ó hÉilidhe
    Tomás Ó hÉilidhe, Dec 12, 2007
    #11
  12. "Tomás Ó hÉilidhe" <> writes:

    > Philip Potter <> wrote in news:fjp525$dj5$:
    >
    >> The important thing is that you do not set a character encoding in
    >> your headers.

    <snip>
    > I started out using a Linux newsreader called Pan, but it turned out to be
    > crap.
    >
    > At the moment I'm using a Windows newsreader called XNews, which is
    > supposedly the best newsreader ever (or so say the people who recommended
    > it to me). I wouldn't have thought that XNews got it wrong when it came to
    > character encoding. . . ?


    I would not have thought so either, but your postings arrive here with
    no encoding specified and characters outside the safe 7-bit range.
    There is probably an XNews group or mailing list where you can ask
    about fixing this.

    --
    Ben.
    Ben Bacarisse, Dec 12, 2007
    #12
  13. George2

    Richard Bos Guest

    Philip Potter <> wrote:

    > "Tom��������������������������������" wrote:
    > > Philip Potter <> wrote in news:fjp0c2$v2j$:
    > >
    > >> Chris Dollin wrote:
    > >>> Tom�s � h�ilidhe wrote:
    > >>>
    > >>> (Those are funny characters in the name.)
    > >> Not as funny as they appear to me:
    > >> "Tom����������������������ï
    > >> ¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½" wrote:

    > >
    > > Do you not use UTF-8 in your newsreader?

    >
    > The important thing is that you do not set a character encoding in your
    > headers. For example:


    The most important thing is that, Welshmen and Friesians
    notwithstanding, Usenet is still a 7-bit medium, and those who assume
    otherwise do so at their own peril, and at the peril of their names'
    spelling.

    Richard
    Richard Bos, Dec 13, 2007
    #13
  14. "Tom��������������������������������" wrote:
    > Philip Potter <> wrote in news:fjp525$dj5$:
    >
    >> The important thing is that you do not set a character encoding in
    >> your headers. For example:
    >>
    >> Content-Type: text/plain; charset=utf-8
    >>
    >> If you don't provide this information, my newsreader cannot magically
    >> know which character set you're using; and you have no reason to
    >> expect it to default to UTF-8 because I might meet someone else who
    >> expects it to default to ISO-8859-1.
    >>
    >> As a matter of fact I do use UTF-8 as default. If I change it to
    >> ISO-8859-1 then I get "Tomás Ó hÉilidhe wrote:" which looks more
    >> likely. So it seems you aren't using UTF-8 at all, but one of the
    >> ISO-8859 family.
    >>
    >> Unfortunately, I don't have any advice for you on how to rectify the
    >> situation because I use a different newsreader. Nevertheless, I wish
    >> you the best of luck fixing it.

    >
    > I started out using a Linux newsreader called Pan, but it turned out to be
    > crap.
    >
    > At the moment I'm using a Windows newsreader called XNews, which is
    > supposedly the best newsreader ever (or so say the people who recommended
    > it to me). I wouldn't have thought that XNews got it wrong when it came to
    > character encoding. . . ?


    It seems that they did. Appealing to reputation can be useful, but it
    doesn't trump the evidence of your postings. Have a look at the full
    headers yourself - notice that most people here will declare a charset
    in their headers, especially if they want to use non-ASCII characters
    like ᩲ‡. A quick google for "xnews charset" turned up this:

    http://tinyurl.com/ypfygj

    which seems to show that XNews' creator considers the software to be
    aimed at himself only, and isn't interested in catering to things he
    isn't interested in. It seems character sets is one of those things.

    There is also a link on that page to a tool which claims to fix this
    problem in XNews.

    Phil
    Philip Potter, Dec 13, 2007
    #14
  15. Philip Potter <> wrote in comp.lang.c:

    > The important thing is that you do not set a character encoding in
    > your headers. For example:
    >
    > Content-Type: text/plain; charset=utf-8



    I manually went into my XNews settings and added "custom headers".

    How does this post look?

    --
    Tomás Ó hÉilidhe
    Tomás Ó hÉilidhe, Dec 13, 2007
    #15
  16. "Tomás Ó hÉilidhe" <> wrote in comp.lang.c:


    > I manually went into my XNews settings and added "custom headers".
    >
    > How does this post look?



    Damn, looks like I didn't enable the new settings.


    How does this post look?


    --
    Tomás Ó hÉilidhe
    Tomás Ó hÉilidhe, Dec 13, 2007
    #16
  17. "Tom��������������������������������" wrote:
    > Philip Potter <> wrote in comp.lang.c:
    >
    >> The important thing is that you do not set a character encoding in
    >> your headers. For example:
    >>
    >> Content-Type: text/plain; charset=utf-8

    >
    >
    > I manually went into my XNews settings and added "custom headers".
    >
    > How does this post look?
    >


    Can't you see for yourself? Find the option in XNews to display all headers.

    In any case, I think the above attribution line speaks for itself.

    Incidentally, your sig comes out as:
    Tomás Ó hÉilidhe
    which is fine. It seems your message body is UTF-8 but your header is
    ISO-whatever-it-is. This is not a good situation.
    Philip Potter, Dec 13, 2007
    #17
  18. OFF-TOPIC Re: how the macro works

    "Tomás Ó hÉilidhe" <> writes:
    Ben Bacarisse, Dec 13, 2007
    #18
  19. On Wed, 12 Dec 2007 17:16:53 +0000, Philip Potter <>
    wrote:

    > "Tom????????????????????????????????" wrote:
    > > Philip Potter <> wrote in news:fjp0c2$v2j$:
    > >
    > >> Chris Dollin wrote:
    > >>> Tom�s � h�ilidhe wrote:
    > >>>
    > >>> (Those are funny characters in the name.)


    > > Do you not use UTF-8 in your newsreader?

    >
    > The important thing is that you do not set a character encoding in your
    > headers. For example:
    >
    > Content-Type: text/plain; charset=utf-8
    >

    Or 8859 or whatever. But that applies to the (single-part) BODY.
    The correct way to use non-7-bit-ASCII IN HEADERS, in particular the
    'From:' header, is =?cset?Q?qp?= or =?cset?B?b64?= per RFC 2047.

    It's probably possible to configure this by hand if your program can't
    (or won't) be fixed; alternatively the OP might consider just using
    the (ASCII) address and an ASCII-compatible nickname, with the real
    name in the sig -- which at protocol level is in the body, as above.
    Or if there is an acceptable ASCII transliteration of the name.

    - formerly david.thompson1 || achar(64) || worldnet.att.net
    David Thompson, Dec 24, 2007
    #19
    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. Dead RAM
    Replies:
    20
    Views:
    1,107
    John Harrison
    Jul 14, 2004
  2. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    576
    Jack Klein
    Sep 21, 2003
  3. sounak

    to get macro name from macro value

    sounak, Nov 22, 2005, in forum: C Programming
    Replies:
    17
    Views:
    500
    Mark McIntyre
    Nov 22, 2005
  4. Patrick Kowalzick
    Replies:
    5
    Views:
    469
    Patrick Kowalzick
    Mar 14, 2006
  5. Mike Manilone

    macro inside macro

    Mike Manilone, Oct 3, 2011, in forum: C Programming
    Replies:
    8
    Views:
    457
    Mike Manilone
    Oct 6, 2011
Loading...

Share This Page