Re: Macro inside a string



Sweta wrote:
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/>.

--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxxx
.



Relevant Pages