whats happening here

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • parag_paul@hotmail.com

    whats happening here

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

    #2
    Re: whats happening here

    On Mar 25, 12:23 pm, "parag_p...@hot mail.com" <parag_p...@hot mail.com>
    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.

    Comment

    • Eric Sosman

      #3
      Re: whats happening here

      bdsatish wrote:
      On Mar 25, 12:23 pm, "parag_p...@hot mail.com" <parag_p...@hot mail.com>
      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
      esosman@ieee-dot-org.invalid

      Comment

      Working...