MACRO QUERY-2

Discussion in 'C Programming' started by Tagore, Jan 8, 2009.

  1. Tagore

    Tagore Guest

    Please consider program given below;

    #include<stdio.h>
    #define f(a,b) a##b
    #define g(a) #a
    #define h(a) g(a)

    int main(void)
    {
    printf("%s\n",h(f(1,2)));
    printf("%s\n",g(f(1,2)));
    return 0;
    }

    above program gives following output :
    12
    f(1,2)


    I expected both printf to give same output but why is it showing
    different outputs?
    Tagore, Jan 8, 2009
    #1
    1. Advertising

  2. Tagore

    Guest

    On Jan 8, 6:59 pm, Tagore <> wrote:
    > Please consider program given below;
    >
    > #include<stdio.h>
    > #define f(a,b) a##b
    > #define g(a) #a
    > #define h(a) g(a)
    >
    > int main(void)
    > {
    >          printf("%s\n",h(f(1,2)));
    >          printf("%s\n",g(f(1,2)));
    >          return 0;
    >
    > }
    >
    > above program gives following output :
    > 12
    > f(1,2)
    >
    > I expected both printf to give same output but why is it showing
    > different outputs?


    See question 10.20 of the C-FAQ. <http://c-faq.com/>
    , Jan 8, 2009
    #2
    1. Advertising

  3. "Tagore" <> wrote in message
    news:...
    > Please consider program given below;
    >
    > #include<stdio.h>
    > #define f(a,b) a##b
    > #define g(a) #a
    > #define h(a) g(a)
    >
    > int main(void)
    > {
    > printf("%s\n",h(f(1,2)));
    > printf("%s\n",g(f(1,2)));
    > return 0;
    > }
    >
    > above program gives following output :
    > 12
    > f(1,2)
    >
    >
    > I expected both printf to give same output but why is it showing
    > different outputs?


    `h' has another level of expansion which actually renders the token `f(1,2)'
    into `12'. However, `g' does not have that extra level, therefore it has no
    chance to render `f(1,2)', so it stays as-is.
    Chris M. Thomasson, Jan 8, 2009
    #3
    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. Dead RAM
    Replies:
    20
    Views:
    1,095
    John Harrison
    Jul 14, 2004
  2. D Senthil Kumar

    macro name from macro?

    D Senthil Kumar, Sep 20, 2003, in forum: C Programming
    Replies:
    1
    Views:
    565
    Jack Klein
    Sep 21, 2003
  3. sounak

    to get macro name from macro value

    sounak, Nov 22, 2005, in forum: C Programming
    Replies:
    17
    Views:
    486
    Mark McIntyre
    Nov 22, 2005
  4. Patrick Kowalzick
    Replies:
    5
    Views:
    459
    Patrick Kowalzick
    Mar 14, 2006
  5. Mike Manilone

    macro inside macro

    Mike Manilone, Oct 3, 2011, in forum: C Programming
    Replies:
    8
    Views:
    440
    Mike Manilone
    Oct 6, 2011
Loading...

Share This Page