Macro inside a string

S

Sweta

Hi,
I have the following code -
#include <stdio.h>
#define VAR i

main()
{
int VAR;
VAR = 10;
printf("VAR = %d", VAR);
}

As of now, this code prints VAR = 10
I want to have the output as i = 10 i.e. the macro replacement should
be printed as string. Does anybody have any idea how we can do it? Do
we have something like #, ## for macros which do not have argument
list.

Thanks,
Sweta
 
E

Eric Sosman

Sweta said:
Hi,
I have the following code -
#include <stdio.h>
#define VAR i

main()
{
int VAR;
VAR = 10;
printf("VAR = %d", VAR);
}

As of now, this code prints VAR = 10
I want to have the output as i = 10 i.e. the macro replacement should
be printed as string. Does anybody have any idea how we can do it? Do
we have something like #, ## for macros which do not have argument
list.

#define STRING(x) STRING2(x)
#define STRING2(x) # x
....
printf (STRING(VAR) " = %d\n", VAR);
/* or */
printf ("%s = %d\n", STRING(VAR), VAR);

See also Question 11.17 in the comp.lang.c FAQ
at <http://www.c-faq.com/>.
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top