Macro replacement occurs from the outside in. (Strictly speaking, the preprocessor is required to behave as though it replaces macros one at a time, starting from the beginning of the file and restarting after each replacement.)
The standard (C99 §6.10.3.2/2) says
If, in the replacement list, a parameter is immediately preceded by a
#
preprocessing token, both are replaced by a single character string literal preprocessing token that contains the spelling of the preprocessing token sequence for the corresponding argument.
Since #
is present in the replacement list for the macro g
, the argument f(1,2)
is converted to a string immediately, and the result is "f(1,2)"
.
On the other hand, in h(f(1,2))
, since the replacement list doesn't contain #
, §6.10.3.1/1 applies,
After the arguments for the invocation of a function-like macro have been identified, argument substitution takes place. A parameter in the replacement list, unless preceded by a
#
or##
preprocessing token or followed by a##
preprocessing token (see below), is replaced by the corresponding argument after all macros contained therein have been expanded.
and the argument f(1, 2)
is macro expanded to give 12
, so the result is g(12)
which then becomes "12"
when the file is "re-scanned".