public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* How to use # inside a macro definition
@ 2003-11-10  7:03 Kapileswar Rao
  2003-11-10 10:21 ` "Gergely, Tamás"
  2003-11-10 12:37 ` Eljay Love-Jensen
  0 siblings, 2 replies; 4+ messages in thread
From: Kapileswar Rao @ 2003-11-10  7:03 UTC (permalink / raw)
  To: gcc-help

Hello ,
   I need some help regarding how to use # inside macro definition in C
macros. I get an error, '#' is not followed by a macro parameter. I
understand the reason but is there no escape character to avoid it
expecting a macro parameter.

An example of my macro definition is

#define exmp(par1,par2) {\
#ifdef EVAL   \
	par1 = par2; \
#endif  \
	} 

Anticipating Help.
thanks and regards,
--Kapil

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to use # inside a macro definition
  2003-11-10  7:03 How to use # inside a macro definition Kapileswar Rao
@ 2003-11-10 10:21 ` "Gergely, Tamás"
  2003-11-10 12:36   ` Kapileswar Rao
  2003-11-10 12:37 ` Eljay Love-Jensen
  1 sibling, 1 reply; 4+ messages in thread
From: "Gergely, Tamás" @ 2003-11-10 10:21 UTC (permalink / raw)
  To: Kapileswar Rao; +Cc: gcc-help

Hello!

I don't know if there is an escape char or not. But if you write:

#ifdef EVAL
#define exmp(par1,par2) { par1 = par2; }
#else
#define exmp(par1,par2) { }
#endif

it will work. As I know, your example practically equals:

#define exmp(par1,par2) { #ifdef EVAL 	par1 = par2; #endif	}

so "#ifdef" and "#endif" are not in the beginning of the lines (where they 
have to be).

gertom

Kapileswar Rao wrote:
> Hello ,
>    I need some help regarding how to use # inside macro definition in C
> macros. I get an error, '#' is not followed by a macro parameter. I
> understand the reason but is there no escape character to avoid it
> expecting a macro parameter.
> 
> An example of my macro definition is
> 
> #define exmp(par1,par2) {\
> #ifdef EVAL   \
> 	par1 = par2; \
> #endif  \
> 	} 
> 
> Anticipating Help.
> thanks and regards,
> --Kapil


-- 
**************************************************************************
* Gergely TamĂĄs                            e-mail:gertom@inf.u-szeged.hu *
* ICQ:104783919                       http://www.inf.u-szeged.hu/~gertom *
**************************************************************************

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to use # inside a macro definition
  2003-11-10 10:21 ` "Gergely, Tamás"
@ 2003-11-10 12:36   ` Kapileswar Rao
  0 siblings, 0 replies; 4+ messages in thread
From: Kapileswar Rao @ 2003-11-10 12:36 UTC (permalink / raw)
  To: "\"\\\"Gergely,\\\" Tamás\\\"\""
  Cc: gcc-help

Hi gertom,
   Thanks for the get around. I knew the get around you gave. Its only
for the sake knowledge I have asked the problem.Earlier I wanted to use
# in some other macro def and I got the same problem and had to go other
way.

thanks and regards,
--Kapil 

On Mon, 2003-11-10 at 15:51, "Gergely, Tamás" wrote:
> Hello!
> 
> I don't know if there is an escape char or not. But if you write:
> 
> #ifdef EVAL
> #define exmp(par1,par2) { par1 = par2; }
> #else
> #define exmp(par1,par2) { }
> #endif
> 
> it will work. As I know, your example practically equals:
> 
> #define exmp(par1,par2) { #ifdef EVAL 	par1 = par2; #endif	}
> 
> so "#ifdef" and "#endif" are not in the beginning of the lines (where they 
> have to be).
> 
> gertom
> 
> Kapileswar Rao wrote:
> > Hello ,
> >    I need some help regarding how to use # inside macro definition in C
> > macros. I get an error, '#' is not followed by a macro parameter. I
> > understand the reason but is there no escape character to avoid it
> > expecting a macro parameter.
> > 
> > An example of my macro definition is
> > 
> > #define exmp(par1,par2) {\
> > #ifdef EVAL   \
> > 	par1 = par2; \
> > #endif  \
> > 	} 
> > 
> > Anticipating Help.
> > thanks and regards,
> > --Kapil
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: How to use # inside a macro definition
  2003-11-10  7:03 How to use # inside a macro definition Kapileswar Rao
  2003-11-10 10:21 ` "Gergely, Tamás"
@ 2003-11-10 12:37 ` Eljay Love-Jensen
  1 sibling, 0 replies; 4+ messages in thread
From: Eljay Love-Jensen @ 2003-11-10 12:37 UTC (permalink / raw)
  To: Kapileswar Rao, gcc-help

Hi Kapil,

You cannot embed preprocessor directives in a macro expansion.  They neither get evaluated at the time of macro definition, nor at the time of instantiation.

Perhaps that's unfortunate.  But that's the way it is.

You'll have to figure out an alternative implementation, such as the one from gertom.

Or you could write your own pre-preprocessor.  If you do opt for this solution, I recommend using Perl as the engine or use lex (i.e., GNU flex).  Instead of writing a parser from scratch -- unless your in it for the sheer fun of tokenizing C/C++.  :-)

HTH,
--Eljay


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-11-10 12:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-10  7:03 How to use # inside a macro definition Kapileswar Rao
2003-11-10 10:21 ` "Gergely, Tamás"
2003-11-10 12:36   ` Kapileswar Rao
2003-11-10 12:37 ` Eljay Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).