How to define C macro

Discussion in 'C Programming' started by Vittal, Jul 3, 2003.

  1. Vittal

    Vittal Guest

    Hello All,

    Here is a small C program,

    main()
    {
    int a= 100;
    float b =99.99;
    TEST(a,%d);
    TEST(b,%f);
    }

    Now I want to write a macro for TEST such that it outputs something like this

    main()
    {
    int a=100;
    float b =99.99;
    printf(" The value of a = %d \n",a);
    printf(" The value of b = %f \n",b);
    }

    I tried to write macro like this, but its not working

    #define TEST(a,b) printf(" The value of a = b \n",a)

    Can somebody help me in this?

    Thanks
    -Vittal
    Vittal, Jul 3, 2003
    #1
    1. Advertising

  2. On Thu, 3 Jul 2003, Vittal wrote:
    >
    > TEST(a,%d);
    > TEST(b,%f);
    >
    > Now I want to write a macro for TEST such that it outputs something like this
    >
    > printf(" The value of a = %d \n",a);
    > printf(" The value of b = %f \n",b);
    >
    > I tried to write macro like this, but its not working
    >
    > #define TEST(a,b) printf(" The value of a = b \n",a)


    Try

    #define TEST(a,b) printf(" The value of " #a " = " #b " \n", a)

    (The syntax #foo is a special preprocessing thingamabob that says
    "take the value of foo and stick it in a string literal." Putting
    two string literals next to each other - "foo" "bar" - concatenates
    them - producing the equivalent of "foobar". [This *only* works with
    compile-time literals!] So the above stringizes 'a' and 'b' and
    sticks them in the string.)

    Untested code, may not work if a or b are macros themselves. I.e.,

    TEST(INT_MAX, %d);

    may do incorrect things. Someone else will post that FAQ. :)

    -Arthur
    Arthur J. O'Dwyer, Jul 3, 2003
    #2
    1. Advertising

  3. Vittal

    Marc Boyer Guest

    In article <>, Vittal wrote:
    > Hello All,
    > #define TEST(a,b) printf(" The value of a = b \n",a)


    #define TEST(a,b) printf("The value of " #a " = " #b "\n", a)

    Interresting question in fact.

    Marc Boyer
    --
    Lying for having sex or lying for making war? Trust US presidents :-(
    Marc Boyer, Jul 3, 2003
    #3
  4. "Vittal" <> wrote in message
    news:...
    > Hello All,
    >
    > Here is a small C program,
    >
    > main()
    > {
    > int a= 100;
    > float b =99.99;
    > TEST(a,%d);
    > TEST(b,%f);
    > }
    >
    > Now I want to write a macro for TEST such that it outputs something like

    this
    >
    > main()
    > {
    > int a=100;
    > float b =99.99;
    > printf(" The value of a = %d \n",a);
    > printf(" The value of b = %f \n",b);
    > }
    >
    > I tried to write macro like this, but its not working
    >
    > #define TEST(a,b) printf(" The value of a = b \n",a)
    >
    > Can somebody help me in this?
    >
    > Thanks
    > -Vittal


    Hi Vittal,

    You can use the define:
    #define TEST(fmt,val) ((void)printf("The value of %s = "fmt"\n",#val,val))
    The format(fmt) is just a string and is concatenated with the rest of the
    strings.
    #val is also a string (so "a" or "b" in your example)
    val is the value.

    Marco

    #include <stdio.h>

    #define TEST(fmt,val) ((void)printf("The value of %s = "fmt"\n",#val,val))

    int main()
    {
    int a=100;
    float b=99.99F;

    TEST("%d",a);
    TEST("%f",b);
    return 0;
    }
    Marco de Boer, Jul 3, 2003
    #4
  5. Vittal

    Dan Pop Guest

    In <> (Vittal) writes:

    >main()
    >{
    >int a= 100;
    >float b =99.99;
    >TEST(a,%d);
    >TEST(b,%f);
    >}
    >
    >Now I want to write a macro for TEST such that it outputs something like this
    >
    >main()
    >{
    >int a=100;
    >float b =99.99;
    >printf(" The value of a = %d \n",a);
    >printf(" The value of b = %f \n",b);
    >}
    >
    >I tried to write macro like this, but its not working
    >
    >#define TEST(a,b) printf(" The value of a = b \n",a)


    Obviously, since the preprocessor doesn't touch the contents of string
    literals.

    >Can somebody help me in this?


    Use the # operator and take advantage of the adjacent string splicing
    feature of C:

    #define TEST(a,b) printf(" The value of " #a " = " #b " \n", a)

    Not very easy to read, but it gets the job done.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Jul 3, 2003
    #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. Dead RAM
    Replies:
    20
    Views:
    1,111
    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:
    579
    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:
    501
    Mark McIntyre
    Nov 22, 2005
  4. Patrick Kowalzick
    Replies:
    5
    Views:
    470
    Patrick Kowalzick
    Mar 14, 2006
  5. Mike Manilone

    macro inside macro

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

Share This Page