Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to define C macro

Reply
Thread Tools

How to define C macro

 
 
Vittal
Guest
Posts: n/a
 
      07-03-2003
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
 
Reply With Quote
 
 
 
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      07-03-2003

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

 
Reply With Quote
 
 
 
 
Marc Boyer
Guest
Posts: n/a
 
      07-03-2003
In article <(E-Mail Removed) >, 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
 
Reply With Quote
 
Marco de Boer
Guest
Posts: n/a
 
      07-03-2003
"Vittal" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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;
}


 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      07-03-2003
In <(E-Mail Removed) > http://www.velocityreviews.com/forums/(E-Mail Removed) (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: (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
About typedef -- define the function pointer or define function model? robin liu C Programming 3 04-21-2006 03:26 PM
#define _ and #define __ Brian Takita Ruby 0 01-23-2006 04:34 AM
#define macro to enclose an older macro with strings Dead RAM C++ 20 07-14-2004 10:58 AM
How to define a define that defines some defines ? theotyflos C Programming 3 02-19-2004 05:07 PM



Advertisments