Macro evaluation order

10,898

Solution 1

From http://gcc.gnu.org/onlinedocs/cpp/Argument-Prescan.html#Argument-Prescan

Macro arguments are completely macro-expanded before they are substituted into a macro body, unless they are stringified or pasted with other tokens. After substitution, the entire macro body, including the substituted arguments, is scanned again for macros to be expanded. The result is that the arguments are scanned twice to expand macro calls in them.

Meaning:

  • f concatenates its argument and so its argument is not expanded
  • h does not stringify or concatenate its argument and so its argument is expanded.
  • g stringifies its argument, and so its argument is not expanded

h(f(1,2)) -> g(12) -> "12"

g(f(1,2)) -> "f(1,2)"

Solution 2

An argument is macro-replaced before it is substituted into the replacement list, except where it appears as the operand of # (stringize) or ## (concatenate).

In your macro h, the parameter a is not an argument of one of those two operators, so the argument is macro-replaced and then substitued into the replacement list. That is, the argument f(1,2) is macro replaced to become 1##2, and then to 12, and then it is substituted into g(12), which is (again) macro-replaced to become "12".

When you invoke g directly, the parameter a is an argument of the # operator, so its argument is not macro-replaced before subsitution: f(1,2) is substituted directly into the replacement list, yielding "f(1,2)".

Solution 3

I'm not sure order of evaluation is a meaningful term for C or C++ macros, because macro expansion happens at compile time

As to why the second output is f(1,2) is is because macros are textual substitution. When g(f(1,2)) is expanded, the argument of g is the sequence of tokens f(1,2) and that get stringified.

Think in terms of the C compiler. In the context of the second printf it reads a g token, recognize that it is a macro at lexing & parsing time then expand that macro invocation. The compiler is basically doing: if the current token is a macro name, then expand it when lexing your code. Macro expansion only happen when possible (so for a macro with arguments requires the  left parenthesis), and is done as soon as possible.

Share:
10,898

Related videos on Youtube

Utkarsh Srivastav
Author by

Utkarsh Srivastav

Updated on June 22, 2022

Comments

  • Utkarsh Srivastav
    Utkarsh Srivastav almost 2 years

    Possible Duplicate:
    # and ## in macros

    why the output of second printf is f(1,2) what is the order in which macro is evaluated?

    #include <stdio.h>
    #define f(a,b) a##b
    #define g(a) #a
    #define h(a) g(a)
    
    int main()
    {
      printf("%s\n",h(f(1,2)));
      printf("%s\n",g(f(1,2)));
      return 0;
    }
    
    output 12 
           f(1,2)
    
  • Pubby
    Pubby over 12 years
    This is not technically correct. The argument would only be a textual substitution if is stringified or concatenated in the macro.
  • Basile Starynkevitch
    Basile Starynkevitch over 12 years
    I'm not sure to understand you. cpp is only a textual rewriting system.
  • Pubby
    Pubby over 12 years
    If macros strictly used textual substitution then h(f(1,2)) should expand to "f(1,2)"
  • Basile Starynkevitch
    Basile Starynkevitch over 12 years
    But that is also textual substitution, as is the standard cpp behavior. The diffeerence is when is a substitution fired.
  • Pubby
    Pubby over 12 years
    That's a better way of putting it. An explanation of when the substitution occurs would be very helpful in your answer.