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