whats happening here

Discussion in 'C Programming' started by parag_paul@hotmail.com, Mar 25, 2008.

  1. 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;
    }
     
    , Mar 25, 2008
    #1
    1. Advertising

  2. bdsatish Guest

    On Mar 25, 12:23 pm, "" <>
    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;
    > }


    f(1,2) ==> 12 because ## is a concatenating operator, which treats its
    inputs a strings. So 1 and 2 are concatenated to form 12

    h(a) is simply #a where # is stringisizing operator. So h(12)
    becomes the string "12"

    Since g(a) is simply #a, the input argument f(1,2) is thrown out as if
    it were a string.
     
    bdsatish, Mar 25, 2008
    #2
    1. Advertising

  3. Guest

    On Mar 25, 12:43 pm, bdsatish <> wrote:
    > On Mar 25, 12:23 pm, "" <>
    > 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;
    > >   }

    >
    > f(1,2) ==> 12 because ## is a concatenating operator, which treats its
    > inputs a strings. So 1 and 2 are concatenated to form 12
    >
    > h(a) is simply #a  where #  is stringisizing operator. So h(12)
    > becomes the string "12"
    >
    > Since g(a) is simply #a, the input argument f(1,2) is thrown out as if
    > it were a string.


    So what is the order of implemenation.
    for h(f(1,2))

    f(1,2)
    happens first
    and in the second case,
    g(X)
    happens first and then f(1,2)
     
    , Mar 25, 2008
    #3
  4. WANG CONG Guest

    wrote:

    > On Mar 25, 12:43 pm, bdsatish <> wrote:
    >> On Mar 25, 12:23 pm, "" <>
    >> 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;
    >> > }

    >>
    >> f(1,2) ==> 12 because ## is a concatenating operator, which treats its
    >> inputs a strings. So 1 and 2 are concatenated to form 12
    >>
    >> h(a) is simply #a  where #  is stringisizing operator. So h(12)
    >> becomes the string "12"
    >>
    >> Since g(a) is simply #a, the input argument f(1,2) is thrown out as if
    >> it were a string.

    >
    > So what is the order of implemenation.
    > for h(f(1,2))
    >
    > f(1,2)
    > happens first
    > and in the second case,
    > g(X)
    > happens first and then f(1,2)


    See Question 11.17 of C FAQ .

    http://c-faq.com/ansi/stringize.html

    --
    Hi, I'm a .signature virus, please copy/paste me to help me spread
    all over the world.
     
    WANG CONG, Mar 25, 2008
    #4
  5. Eric Sosman Guest

    bdsatish wrote:
    > On Mar 25, 12:23 pm, "" <>
    > 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;
    >> }

    >
    > f(1,2) ==> 12 because ## is a concatenating operator,


    Yes.

    > which treats its
    > inputs a strings.


    No.

    See WANG CONG's response.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Mar 25, 2008
    #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. Replies:
    3
    Views:
    411
    Howard
    Feb 22, 2005
  2. dan miller (moderator, s.p.d)

    what is happening here?

    dan miller (moderator, s.p.d), Apr 6, 2004, in forum: Python
    Replies:
    3
    Views:
    356
  3. vikramtheone

    Basics of VHDL. Whats happening here?

    vikramtheone, Jun 1, 2009, in forum: VHDL
    Replies:
    1
    Views:
    500
    jeppe
    Jun 1, 2009
  4. tschmittldk
    Replies:
    8
    Views:
    380
    Jorgen Grahn
    Dec 29, 2010
  5. George George

    what is happening here

    George George, Apr 8, 2009, in forum: Ruby
    Replies:
    6
    Views:
    137
    George George
    Apr 9, 2009
Loading...

Share This Page