Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   How to define C macro (http://www.velocityreviews.com/forums/t313981-how-to-define-c-macro.html)

Vittal 07-03-2003 01:38 PM

How to define C macro
 
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

Arthur J. O'Dwyer 07-03-2003 02:32 PM

Re: How to define C macro
 

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


Marc Boyer 07-03-2003 02:41 PM

Re: How to define C macro
 
In article <f9dcc290.0307030538.2d9d637a@posting.google.com >, 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 :-(

Marco de Boer 07-03-2003 02:43 PM

Re: How to define C macro
 
"Vittal" <vsnadagouda@yahoo.com> wrote in message
news:f9dcc290.0307030538.2d9d637a@posting.google.c om...
> 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;
}



Dan Pop 07-03-2003 04:30 PM

Re: How to define C macro
 
In <f9dcc290.0307030538.2d9d637a@posting.google.com > vsnadagouda@yahoo.com (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@ifh.de


All times are GMT. The time now is 04:11 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57