Thread: Nested Macros Evaluation??

  1. #1
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Question Nested Macros Evaluation??

    Code:
    #include "stdio.h"
    #define a(x,y) x##y
    #define b(x) #x
    #define c(x) b(x)
    int main()
    {
     printf("%s\t",c(a(34,56)));
     printf("%s\n",b(a(34,56)));
     return 0;
    }
     
    Output:
    3456 a(34,56)
    Doubt:
    According to my understanding c(a(34,56)) should be expanded to c(3456) and then to b(3456) followed by "3456"
    and b(a(34,56)) should be expanded to b(3456) followed by "3456". So the output should be:
    3456 3456 but this is not so why?
    If a statement contains a macro within macro then what would be the order of expansion in macros? Is it that first the inner one would be expanded followed by the outer one or vice versa?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, because b() expands to the input as a string - not to the expansion of the input. The #x is the "magic" part here - the # means "immediately, translate x to a string", not "expand x, then translate to a string".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58
    Thanks matsp..

    Still i m having doubt....

    Code:
    that means outerone will be exec'd first.. if so, 
    
    first one should also leads to a(34,56)... how 3456 is printed ??..
    
    c(a(34,56)) =>b(a(34,56)) =>a(34,56)

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    so in the c case, x is expanded before b() is expanded, which means that you get 3456 before you get to b().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Only one level of variable substitution is conducted at each expansion. This means that this:

    Code:
    #define TEST foo
    #define A(X) #X
    
    A(TEST)
    Will expand to "TEST", not "foo". To expand to "foo" you must force another substitution:

    Code:
    #define TEST foo
    #define A(X) AA(X)
    #define AA(X) #X
    
    A(TEST)
    Now expands to "foo"
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested array vs. tree
    By KONI in forum Tech Board
    Replies: 1
    Last Post: 06-07-2007, 04:43 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Nested Classes
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2004, 11:57 AM
  4. Evaluation of nested loops
    By Mister C in forum C Programming
    Replies: 2
    Last Post: 08-13-2004, 01:47 PM
  5. Performance Evaluation
    By rick barclay in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 09-16-2001, 10:16 AM