Forcing a keyword to be treated as function

Discussion in 'C Programming' started by spibou@gmail.com, Jun 5, 2006.

  1. Guest

    I read in the "ctype.h - macros or functions?" thread that
    if foo is both the name of a function and a macro , by typing
    (foo) you instruct the compiler to use the function version.
    Can someone explain to me why this works ?

    Cheers
    Spiros Bousbouras
     
    , Jun 5, 2006
    #1
    1. Advertising

  2. Michael Mair Guest

    schrieb:
    > I read in the "ctype.h - macros or functions?" thread that
    > if foo is both the name of a function and a macro , by typing
    > (foo) you instruct the compiler to use the function version.


    Consider
    ,-- macrofun.c -
    #include <stdio.h>
    #define bar baz
    #define foo(s) bar(s)

    void (foo) (const char *s);
    void (bar) (const char *s);

    int main (void)
    {
    foo("Hello");
    (foo)("world");

    return 0;
    }

    void (foo) (const char *s)
    {
    printf(" *%s*\n", s);
    }

    void baz (const char *s)
    {
    printf("%s", s);
    }

    `---
    and play with the parentheses around foo for the three locations
    and baz<->foo


    > Can someone explain to me why this works ?


    The short of it: The C preprocessor performs text replacement;
    "(foo)(something)" does not match the pattern "foo(s)",
    "foo(something)" does.

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Jun 5, 2006
    #2
    1. Advertising

  3. Guest

    Michael Mair wrote:

    > The short of it: The C preprocessor performs text replacement;
    > "(foo)(something)" does not match the pattern "foo(s)",
    > "foo(something)" does.


    How is "(foo)(something)" tokenized ?

    Cheers
    Spiros Bousbouras
     
    , Jun 5, 2006
    #3
  4. Eric Sosman Guest

    wrote On 06/05/06 16:05,:
    > I read in the "ctype.h - macros or functions?" thread that
    > if foo is both the name of a function and a macro , by typing
    > (foo) you instruct the compiler to use the function version.
    > Can someone explain to me why this works ?


    It's because the macro must be a "function-like" macro,
    that is, one like

    #define sqrt(x) __builtin_sqrt(x)

    and not like

    #define sqrt __builtin_sqrt

    The preprocessor will not recognize and expand a function-
    like macro unless the name is followed by a ( that introduces
    the list of arguments. When you write (sqrt)(42.0) the `sqrt'
    is followed by a ) and not by a (, so the preprocessor leaves
    it alone and does not try to expand it as a macro.

    --
     
    Eric Sosman, Jun 5, 2006
    #4
  5. writes:
    > Michael Mair wrote:
    >> The short of it: The C preprocessor performs text replacement;
    >> "(foo)(something)" does not match the pattern "foo(s)",
    >> "foo(something)" does.

    >
    > How is "(foo)(something)" tokenized ?


    It's the following sequence of tokens:
    left-paren
    identifier "foo"
    right-paren
    left-paren
    something (whatever that happens to be)
    right-paren

    Since the answer to your question is so obvious and straightforward, I
    suspect you were really trying to ask something else.

    --
    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, Jun 5, 2006
    #5
  6. Guest

    Keith Thompson wrote:


    >
    > It's the following sequence of tokens:
    > left-paren
    > identifier "foo"
    > right-paren
    > left-paren
    > something (whatever that happens to be)
    > right-paren
    >
    > Since the answer to your question is so obvious and straightforward, I
    > suspect you were really trying to ask something else.


    Oh no , I asked what I wanted to ask and in fact the tokenization is
    what I thought would happen. But I guess I had in the back of my head
    the notion that if a token is the name of a macro , it will be
    expanded. But
    Eric Sosman explained why this would not happen in this case.

    Cheers
    Spiros Bousbouras
     
    , Jun 5, 2006
    #6
    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. Jim Heavey
    Replies:
    1
    Views:
    378
    Oisín Grehan
    Dec 2, 2003
  2. Dmitriy Zakharov
    Replies:
    2
    Views:
    440
    Dmitriy Zakharov
    Jul 30, 2004
  3. Scott J. McCaughrin

    static data-member treated as extern

    Scott J. McCaughrin, Nov 8, 2004, in forum: C++
    Replies:
    2
    Views:
    491
    John Harrison
    Nov 9, 2004
  4. Susan Rice
    Replies:
    18
    Views:
    510
    Frederick Gotham
    Nov 5, 2006
  5. Eugene
    Replies:
    6
    Views:
    768
    Ken Cox [Microsoft MVP]
    Nov 11, 2004
Loading...

Share This Page