Is it standard compliant?

Discussion in 'C Programming' started by Nishu, Sep 22, 2006.

  1. Nishu

    Nishu Guest

    Hi all,

    I was under the impression that below code should give errors while
    compiling, since i'm using same name for macro as well as function. but
    it didnt give any errors!!
    Isnt this is correct that pre-processors are processed before compiling
    and wherever the "matched" label is found, pre-processor replaces it
    with corresponding code snippet.
    Is it defined in standard that scope of pre-processor starts only for
    the code which occurs after its definition? (Now, In practice, I AVOID
    using macros in small letters. Its just an example to clarify my
    doubt.)

    #include<stdio.h>

    int min(int x, int y)
    {
    return (((x) < (y))? (x) : (y));
    }

    int main(void)
    {
    int i, j, k, l;
    i = 1;
    j =2;
    k = min(i,j);

    #define min(x,y) (((x) < (y))? (x) : (y))

    l = min(i,j);
    printf("k = %d l = %d", k, l);

    return 0;
    }

    Thanks.
    -Nishu
    Nishu, Sep 22, 2006
    #1
    1. Advertising

  2. Nishu

    Michael Mair Guest

    Nishu schrieb:
    > I was under the impression that below code should give errors while
    > compiling, since i'm using same name for macro as well as function. but
    > it didnt give any errors!!


    Apart from library identifiers which are allowed to belong to a
    function-like macro or to a function, there is no problem here.
    Due to the different translation phases, there is a well-defined
    albeit possibly unexpected result (which is, in this case, good
    enough).

    > Isnt this is correct that pre-processors are processed before compiling
    > and wherever the "matched" label is found, pre-processor replaces it
    > with corresponding code snippet.


    A macro is known from the place of its definition to the end
    of the translation unit (or to a corresponding #undef directive),
    so there is a "macro defined" and "macro undefined" part of
    your translation unit. Only in the former, replacement takes
    place.

    > Is it defined in standard that scope of pre-processor starts only for
    > the code which occurs after its definition? (Now, In practice, I AVOID
    > using macros in small letters. Its just an example to clarify my
    > doubt.)


    Essentially, yes.


    > #include<stdio.h>
    >
    > int min(int x, int y)
    > {


    Mark this one with
    puts("function");

    > return (((x) < (y))? (x) : (y));
    > }
    >
    > int main(void)
    > {
    > int i, j, k, l;
    > i = 1;
    > j =2;
    > k = min(i,j);
    >
    > #define min(x,y) (((x) < (y))? (x) : (y))


    Mark this one with
    #define min(x, y) \
    (puts("macro"), \
    (((x) < (y))? (x) : (y)) \
    )

    > l = min(i,j);


    FWIW, you can "protect" function calls against macro
    replacement: Insert
    (min)(i, j);
    to see what it prints.

    > printf("k = %d l = %d", k, l);
    >
    > return 0;
    > }


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

  3. Nishu

    Nishu Guest

    Michael Mair wrote:
    --snip-
    > > Is it defined in standard that scope of pre-processor starts only for
    > > the code which occurs after its definition? (Now, In practice, I AVOID
    > > using macros in small letters. Its just an example to clarify my
    > > doubt.)

    >
    > Essentially, yes.
    >
    >
    > > #include<stdio.h>
    > >
    > > int min(int x, int y)
    > > {

    >
    > Mark this one with
    > puts("function");
    >
    > > return (((x) < (y))? (x) : (y));
    > > }
    > >
    > > int main(void)
    > > {
    > > int i, j, k, l;
    > > i = 1;
    > > j =2;
    > > k = min(i,j);
    > >
    > > #define min(x,y) (((x) < (y))? (x) : (y))

    >
    > Mark this one with
    > #define min(x, y) \
    > (puts("macro"), \
    > (((x) < (y))? (x) : (y)) \
    > )
    >
    > > l = min(i,j);

    >
    > FWIW, you can "protect" function calls against macro
    > replacement: Insert
    > (min)(i, j);
    > to see what it prints.
    >


    Thanks Michael; and I understood that doing (min)(i,j); invokes the
    function address call instead of macro.
    -Regards
    Nishu
    Nishu, Sep 22, 2006
    #3
    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. =?Utf-8?B?Zm1AbmV3c2dyb3Vwcy5ub3NwYW0=?=

    508 compliant (i.e. accessible to the people with disabilities)?

    =?Utf-8?B?Zm1AbmV3c2dyb3Vwcy5ub3NwYW0=?=, Nov 4, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    483
    Steven Cheng[MSFT]
    Nov 8, 2004
  2. John Grandy

    project not compliant with ASP.NET 1.1

    John Grandy, Sep 12, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    355
    John Grandy
    Sep 12, 2005
  3. David Mathog

    Standard compliant seek with long long?

    David Mathog, Oct 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    347
    Walter Roberson
    Oct 20, 2005
  4. Andrew Poulos

    SWF methods and standard compliant XHTML

    Andrew Poulos, Dec 19, 2005, in forum: Javascript
    Replies:
    0
    Views:
    71
    Andrew Poulos
    Dec 19, 2005
  5. revek
    Replies:
    15
    Views:
    152
    Thomas 'PointedEars' Lahn
    Feb 2, 2009
Loading...

Share This Page