want to know hoe the following works .

Discussion in 'C Programming' started by maadhuu, Aug 13, 2005.

  1. maadhuu

    maadhuu Guest

    #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;
    }

    the output of this is
    12
    f(1,2)
    on executing the code .Can someone pleease tell me how this works ??
    Thanking you,
    ranjan.
    maadhuu, Aug 13, 2005
    #1
    1. Advertising

  2. maadhuu

    Guest

    Hi Ranjan,

    If an occurrence of a parameter in the replacement token is preceded by
    #, quotes (") will be placed around the corresponding parameter.

    h(f(1,2)) ===> first f(1,2) will be expanded as 12 ===> h(12) ===>
    g(12) ===> "12"
    g(f(1,2)) ===> "f(1,2)"

    Regards,
    Raju

    maadhuu wrote:
    > #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;
    > }
    >
    > the output of this is
    > 12
    > f(1,2)
    > on executing the code .Can someone pleease tell me how this works ??
    > Thanking you,
    > ranjan.
    , Aug 13, 2005
    #2
    1. Advertising

  3. maadhuu

    Michael Mair Guest

    maadhuu wrote:
    > #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;
    > }
    >
    > the output of this is
    > 12
    > f(1,2)
    > on executing the code .Can someone pleease tell me how this works ??
    > Thanking you,
    > ranjan.


    Read a C book on the preprocessor operators # (stringize) and
    ## (concat) for details; the former creates a string literal
    from the operand, the latter concatenates its operands.
    The # operator needs to be wrapped once (here by h) in order
    to "evaluate" the operand before stringizing it.

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Aug 13, 2005
    #3
  4. maadhuu

    maadhuu Guest

    hi, i still have a doubt . i know that "#" is the stringinizing operator
    and "##" is the concatenation operator........now, if the inner operation
    is performed first as in, in the 1st case f(1,2) and in the 2nd case also,
    f(1,2) and then the outer operation, don't you think that the output should
    be the same .........i want to know how does one extra layer in case of
    h(f(1,2)) make the difference ........thanking you, coz' it seemed to me
    that both h(f(1,2)) and g(f(1,2)) should give the same output.
    thanking you, once again.
    ranjan.
    maadhuu, Aug 13, 2005
    #4
  5. maadhuu

    Michael Mair Guest

    maadhuu wrote:
    > hi, i still have a doubt . i know that "#" is the stringinizing operator
    > and "##" is the concatenation operator........now, if the inner operation
    > is performed first as in, in the 1st case f(1,2) and in the 2nd case also,
    > f(1,2) and then the outer operation, don't you think that the output should
    > be the same .........i want to know how does one extra layer in case of
    > h(f(1,2)) make the difference ........thanking you, coz' it seemed to me
    > that both h(f(1,2)) and g(f(1,2)) should give the same output.


    Please provide _context_!
    Without it, you make it unnecessarily hard for other people to help you.

    Your original message contained
    ,----
    |#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;
    | }
    `----

    The "extra layer" makes all the difference because preprocessor
    operation is essentially text replacement:
    g(f(1,2)) becomes #f(1,2) because this is how the # operator works.
    h(f(1,2)) becomes g("Expand(f(1,2))") i.e. g(12) because this is
    how macro treatment in the absence of # works.
    So, the additional "layer" leads to actual expansion of f(1,2).


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Aug 13, 2005
    #5
    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. amit129
    Replies:
    2
    Views:
    209
    Szabolcs Borsanyi
    May 27, 2008
  2. amit129
    Replies:
    0
    Views:
    247
    amit129
    May 27, 2008
  3. pluskid
    Replies:
    8
    Views:
    152
    Judson Lester
    Feb 5, 2008
  4. Simon Egginton

    LOOK! i just want to know does anyone know...

    Simon Egginton, Jul 26, 2004, in forum: Javascript
    Replies:
    3
    Views:
    167
    Dr John Stockton
    Jul 26, 2004
  5. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    221
    Gregory Toomey
    Apr 23, 2004
Loading...

Share This Page