macro with the same name in translation unit

Discussion in 'C Programming' started by Mark, Sep 28, 2011.

  1. Mark

    Mark Guest

    Hi,

    consider the snippet:

    #include <stdio.h>

    #define DUMMY 100
    #define FOO 200
    #define DUMMY 200

    int main (void)
    {
    printf("%d, %d\n",FOO, DUMMY);
    return 0;
    }

    The compiler I'm using (gcc-4.1.2) emits warning message about redefined
    macro, but picks up and prints the last defined, i.e. with value 200. Is
    this platform dependent behavior, or undefined behavior?

    Thanks.

    Mark
     
    Mark, Sep 28, 2011
    #1
    1. Advertising

  2. Mark

    James Kuyper Guest

    On 09/28/2011 10:53 AM, Mark wrote:
    > Hi,
    >
    > consider the snippet:
    >
    > #include <stdio.h>
    >
    > #define DUMMY 100
    > #define FOO 200
    > #define DUMMY 200
    >
    > int main (void)
    > {
    > printf("%d, %d\n",FOO, DUMMY);
    > return 0;
    > }
    >
    > The compiler I'm using (gcc-4.1.2) emits warning message about redefined
    > macro, but picks up and prints the last defined, i.e. with value 200. Is
    > this platform dependent behavior, or undefined behavior?


    6.103p2 says:
    > An identifier currently defined as an object-like macro shall not be redefined by another
    > #define preprocessing directive unless the second definition is an object-like macro
    > definition and the two replacement lists are identical. ...


    Both #defines of DUMMY define it as an object-like macro, but the
    replacement lists are not identical. This "shall" occurs in a
    Constraints section, so a diagnostic is required. The warning message
    meets that requirement.

    A conforming implementation of C is allowed, but not required, to accept
    the code after issuing the diagnostic. If the code is accepted, the
    behavior of the program when executed is not defined by the C standard.
    gcc-4.1.2 apparently has chosen one of the two most obvious ways of
    dealing with this situation: letting the the second value override the
    first. The other most obvious way is to keep the value as 100. Either
    option is permitted, as well as many other options that could be much
    nastier. I do NOT recommend relying upon this behavior.

    The following two alternatives don't violate that constraint, and have
    behavior corresponding to the two "obvious ways" I mentioned above:

    Option 1:
    #ifdef DUMMY
    #undef DUMMY
    #endif
    #define DUMMY 200

    Option 2:
    #ifndef DUMMY
    #define DUMMY 200
    #endif
     
    James Kuyper, Sep 28, 2011
    #2
    1. Advertising

  3. James Kuyper <> writes:
    [...]
    > The following two alternatives don't violate that constraint, and have
    > behavior corresponding to the two "obvious ways" I mentioned above:
    >
    > Option 1:
    > #ifdef DUMMY
    > #undef DUMMY
    > #endif
    > #define DUMMY 200
    >
    > Option 2:
    > #ifndef DUMMY
    > #define DUMMY 200
    > #endif


    Option 3:

    #undef DUMMY
    #define DUMMY 200

    #undef does nothing if the macro isn't currently defined (though I
    suppose some compilers might issue a warning).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Sep 28, 2011
    #3
  4. Mark

    James Kuyper Guest

    On 09/28/2011 12:22 PM, Keith Thompson wrote:
    > James Kuyper <> writes:
    > [...]
    >> The following two alternatives don't violate that constraint, and have
    >> behavior corresponding to the two "obvious ways" I mentioned above:
    >>
    >> Option 1:
    >> #ifdef DUMMY
    >> #undef DUMMY
    >> #endif
    >> #define DUMMY 200
    >>
    >> Option 2:
    >> #ifndef DUMMY
    >> #define DUMMY 200
    >> #endif

    >
    > Option 3:


    This is more like Option 1a:

    > #undef DUMMY
    > #define DUMMY 200
    >
    > #undef does nothing if the macro isn't currently defined (though I
    > suppose some compilers might issue a warning).


    I should have remembered that! I still have fond memories of pointing
    out the same issue to Nick Maclauren nearly two decades ago. It was one
    of the few times I ever successfully corrected him on any issue.
     
    James Kuyper, Sep 28, 2011
    #4
  5. James Kuyper <> writes:
    [...]
    > I should have remembered that! I still have fond memories of pointing
    > out the same issue to Nick Maclauren nearly two decades ago. It was one
    > of the few times I ever successfully corrected him on any issue.


    You mean Nick Maclaren? :cool:}

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Sep 28, 2011
    #5
  6. Mark

    James Kuyper Guest

    On 09/28/2011 04:42 PM, Keith Thompson wrote:
    > James Kuyper <> writes:
    > [...]
    >> I should have remembered that! I still have fond memories of pointing
    >> out the same issue to Nick Maclauren nearly two decades ago. It was one
    >> of the few times I ever successfully corrected him on any issue.

    >
    > You mean Nick Maclaren? :cool:}


    Yes. I even looked it up before posting, to make sure I was remembering
    the right person, but I made the mistake of copying his name by hand,
    rather than using cut-and-paste.
     
    James Kuyper, Sep 28, 2011
    #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. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    582
    Jack Klein
    Sep 21, 2003
  2. Chris Croughton

    Is an empty translation unit valid?

    Chris Croughton, Jun 28, 2005, in forum: C Programming
    Replies:
    4
    Views:
    337
    Kenny McCormack
    Jul 3, 2005
  3. sounak

    to get macro name from macro value

    sounak, Nov 22, 2005, in forum: C Programming
    Replies:
    17
    Views:
    504
    Mark McIntyre
    Nov 22, 2005
  4. aaragon
    Replies:
    4
    Views:
    301
    aaragon
    Nov 26, 2007
  5. Tomás Ó hÉilidhe

    Read-only outside translation unit

    Tomás Ó hÉilidhe, Dec 8, 2007, in forum: C Programming
    Replies:
    4
    Views:
    316
    Eric Sosman
    Dec 8, 2007
Loading...

Share This Page