Order of preprocessor macro replacement

Discussion in 'C Programming' started by Shriramana Sharma, Jun 15, 2013.

  1. Hello. I found that the C standard prescribes that once a translation unit has undergone macro replacement once, it will be rescanned for further macros to be replacement and again and again until there are no more macros to replace.

    Due to this rescanning, I can write:

    # define MyInt int
    # define int short
    MyInt a ;

    or:

    # define int short
    # define MyInt int
    MyInt a ;

    and in both cases the final output of the preprocessor is:

    short a ;

    My question is: in which of the above inputs i.e. later definition modifies output of earlier definition or vice versa would the preprocessor arrive at "no more macros to replace" status quicker?
    Shriramana Sharma, Jun 15, 2013
    #1
    1. Advertising

  2. Shriramana Sharma

    Eric Sosman Guest

    On 6/15/2013 10:27 AM, Shriramana Sharma wrote:
    > Hello. I found that the C standard prescribes that once a translation unit has undergone macro replacement once, it will be rescanned for further macros to be replacement and again and again until there are no more macros to replace.
    >
    > Due to this rescanning, I can write:
    >
    > # define MyInt int
    > # define int short
    > MyInt a ;
    >
    > or:
    >
    > # define int short
    > # define MyInt int
    > MyInt a ;
    >
    > and in both cases the final output of the preprocessor is:
    >
    > short a ;


    Aside: You shouldn't #define keywords. If you do so before
    #include'ing any standard header or before expanding any macro
    #define'd by a standard header, the outcome is undefined (7.1.2p4).
    That is, after `#define int short' you must not

    1) #include any standard header whatsoever, or
    2) invoke any macro #define'd by a standard header

    Note that since the standard headers may declare "masking" macros
    for any or all standard library functions, you cannot even call
    sqrt(x); you must write (sqrt)(x) or use some other subterfuge to
    avoid the possibility of a macro expansion.

    In brief: Don't Do That.

    > My question is: in which of the above inputs i.e. later definition modifies output of earlier definition or vice versa would the preprocessor arrive at "no more macros to replace" status quicker?


    "Quicker" would be a function of the particular compiler, and
    of its internal algorithms for recognizing, expanding, and rescanning
    macros. Both formulations require two recognize/expand operations.
    If your question is "Is the second equivalent to `#define MyInt short'"
    the answer is "No," as you can demonstrate with

    #define int short // Don't Do That
    #define MyInt int
    MyInt a;
    #undef int
    MyInt b;
    #define int zaphod // Don't Do That
    MyInt c;

    *My* question would be: Why do you care?

    --
    Eric Sosman
    d
    Eric Sosman, Jun 15, 2013
    #2
    1. Advertising

  3. Shriramana Sharma <> writes:
    > Hello. I found that the C standard prescribes that once a translation
    > unit has undergone macro replacement once, it will be rescanned for
    > further macros to be replacement and again and again until there are
    > no more macros to replace.
    >
    > Due to this rescanning, I can write:
    >
    > # define MyInt int
    > # define int short
    > MyInt a ;
    >
    > or:
    >
    > # define int short
    > # define MyInt int
    > MyInt a ;
    >
    > and in both cases the final output of the preprocessor is:
    >
    > short a ;
    >
    > My question is: in which of the above inputs i.e. later definition
    > modifies output of earlier definition or vice versa would the
    > preprocessor arrive at "no more macros to replace" status quicker?


    Your compiler will spend far less time processing the macro expansions
    than you've spent wondering about it.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 16, 2013
    #3
  4. Shriramana Sharma

    Walter Banks Guest

    Shriramana Sharma wrote:

    > Hello. I found that the C standard prescribes that once a


    > translation unit has undergone macro replacement once,


    > it will be rescanned for further macros to be replacement


    > and again and again until there are no more macros to replace.
    >
    > Due to this rescanning, I can write:
    >
    > # define MyInt int
    > # define int short
    > MyInt a ;
    >
    > or:
    >
    > # define int short
    > # define MyInt int
    > MyInt a ;
    >
    > and in both cases the final output of the preprocessor is:
    >
    > short a ;
    >
    > My question is: in which of the above inputs i.e. later


    > definition modifies output of earlier definition or vice versa


    > would the preprocessor arrive at "no more macros


    > to replace" status quicker?


    It really doesn't matter. The definitions are stored before
    MyInt a; is expanded and it is essentially expanded left
    to right is a single pass by the compiler as it is fetching
    new tokens from the input. In our compilers definition
    order is unimportant.

    Definitions are not pre expanded, as obvious as that
    is as a compiler optimization everyone (including me)
    who has written compilers eventually learn that it is a
    bad idea.

    w..
    Walter Banks, Jun 17, 2013
    #4
    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. qazmlp
    Replies:
    18
    Views:
    559
    Martin Dickopp
    Aug 16, 2003
  2. Cronus
    Replies:
    1
    Views:
    667
    Paul Mensonides
    Jul 15, 2004
  3. qazmlp
    Replies:
    18
    Views:
    838
    Martin Dickopp
    Aug 16, 2003
  4. Charlie Zender

    C-preprocessor macro question

    Charlie Zender, Dec 24, 2003, in forum: C Programming
    Replies:
    3
    Views:
    459
    Richard Bos
    Dec 24, 2003
  5. Eric

    preprocessor q: impossible macro?

    Eric, Jul 18, 2005, in forum: C Programming
    Replies:
    21
    Views:
    658
Loading...

Share This Page