macros

R

raghu

Hello Everyone,

consider the program below

#define DISPLAY(frmt, args...) printf(#args" = ");\
printf(frmt, ##args)
struct _as
{
int a;
int b;
char c;
}g;
main()
{
g.a = 10;
g.b = 6;
g.c = 'R';
DISPLAY(" %d \n", g.a);
DISPLAY(" %d \n", g.b);
DISPLAY(" %c\n", g.c);
}

output will be
g.a = 10
g.b = 6
g.c = R

But if the code is modified as

DISPLAY(" %d \n%d \n%c\n", g.a, g.b, g.c);

instead of all three statements for macros then output will look like
g.a = g.b = g.c = 10
6
R

But the output what I need should be in the format as given below only
for that statement also
g.a = 10
g.b = 6
g.c = R

Is their any way to do that?

Awaiting for the reply.

Thanking you

-Raghu
 
W

Walter Roberson

raghu said:
consider the program below
#define DISPLAY(frmt, args...) printf(#args" = ");\
printf(frmt, ##args)
But if the code is modified as
DISPLAY(" %d \n%d \n%c\n", g.a, g.b, g.c);
instead of all three statements for macros then output will look like
g.a = g.b = g.c = 10
6
R
But the output what I need should be in the format as given below only
for that statement also
g.a = 10
g.b = 6
g.c = R
Is their any way to do that?

In a word, NO. There is no way to do that in standard C. The preprocessor
does not offer any way of iterating over the individual arguments
of a macro.

If you need this sort of facility, you will need to use another
layer of preprocessor, outside of C, such as using the m4 preprocessor.
 
D

deepak

Hello Everyone,

consider the program below

#define DISPLAY(frmt, args...) printf(#args" = ");\
printf(frmt, ##args)
struct _as
{
int a;
int b;
char c;}g;

main()
{
g.a = 10;
g.b = 6;
g.c = 'R';
DISPLAY(" %d \n", g.a);
DISPLAY(" %d \n", g.b);
DISPLAY(" %c\n", g.c);

}

output will be
g.a = 10
g.b = 6
g.c = R

But if the code is modified as

DISPLAY(" %d \n%d \n%c\n", g.a, g.b, g.c);

instead of all three statements for macros then output will look like
g.a = g.b = g.c = 10
6
R

But the output what I need should be in the format as given below only
for that statement also
g.a = 10
g.b = 6
g.c = R

Is their any way to do that?

Awaiting for the reply.

Thanking you

-Raghu

I think it's impossible with a single DISPLAY if sticks with the first
printf in macro.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top