# character in macros

V

Venky

Hello,

I have situation to put # character in macros.

Eg:

I have to include file1 ,file2, and file3 in a file.
#ifndef file1
#include<file1.h>
#endif

#ifndef file2
#include<file2.h>
#endif

#ifndef file3
#include<file3.h>
#endif

I want to add a macro to do the above task

#define INCLUDE(x) #ifndef #x \
#include<#x.h> \
#endif

and then i want to use INCLUDE(file1)
INCLUDE(file2) ...etc.,

But as you all know, macro thinks that all "#" are for stringizing
characters. But i want to tell that macro that this # is special
character and don't treat it as stringizing character.

Any help ?
Thanks,
Venkatesh.
 
S

Spade

Hello,

I have situation to put # character in macros.

Eg:

I have to include file1 ,file2, and file3 in a file.
#ifndef file1
#include<file1.h>
#endif

#ifndef file2
#include<file2.h>
#endif

#ifndef file3
#include<file3.h>
#endif

I want to add a macro to do the above task

#define INCLUDE(x) #ifndef #x \
#include<#x.h> \
#endif

and then i want to use INCLUDE(file1)
INCLUDE(file2) ...etc.,

But as you all know, macro thinks that all "#" are for stringizing
characters. But i want to tell that macro that this # is special
character and don't treat it as stringizing character.

Any help ?


AFAIK, its not possible to have macro expand to another preprocessor
directive.
 
F

Flash Gordon

Spade wrote, On 04/07/07 08:53:
AFAIK, its not possible to have macro expand to another preprocessor
directive.

You are correct, it is not possible. The correct was to handle the
problem the OP is showing is to put the include guards in the include
files themselves.
 
L

Laurent Deniau

Venky said:
Hello,

I have situation to put # character in macros.

Eg:

I have to include file1 ,file2, and file3 in a file.
#ifndef file1
#include<file1.h>
#endif

#ifndef file2
#include<file2.h>
#endif

#ifndef file3
#include<file3.h>
#endif

I want to add a macro to do the above task

#define INCLUDE(x) #ifndef #x \
#include<#x.h> \
#endif

and then i want to use INCLUDE(file1)
INCLUDE(file2) ...etc.,

But as you all know, macro thinks that all "#" are for stringizing
characters. But i want to tell that macro that this # is special
character and don't treat it as stringizing character.

Any help ?

Unless you run the preprocessor twice, this is not possible.

a+, ld.
 
C

Cedric Roux

Venky said:
Hello,

I have situation to put # character in macros.

Eg:

I have to include file1 ,file2, and file3 in a file.
#ifndef file1
#include<file1.h>
#endif

#ifndef file2
#include<file2.h>
#endif

#ifndef file3
#include<file3.h>
#endif

I want to add a macro to do the above task

#define INCLUDE(x) #ifndef #x \
#include<#x.h> \
#endif

and then i want to use INCLUDE(file1)
INCLUDE(file2) ...etc.,

But as you all know, macro thinks that all "#" are for stringizing
characters. But i want to tell that macro that this # is special
character and don't treat it as stringizing character.

Any help ?
Thanks,
Venkatesh.

Note also that "backslash followed by newline" is deleted by
the compiler, so you cannot define a macro that will expand on
several lines. There is no way to tell the preprocessor that it
should insert a newline at this specific point of the macro
replacement.

Cedric.
 
S

SM Ryan

# #define INCLUDE(x) #ifndef #x \
# #include<#x.h> \
# #endif
#
# and then i want to use INCLUDE(file1)
# INCLUDE(file2) ...etc.,

cpp barely qualifies as a macro processor. What you are trying
to do is not guarenteed to work, and rarely does. You either
live with the severe limitations of cpp or use a real macro
processor, such as m4 and many other available.
 
K

Keith Thompson

SM Ryan said:
# #define INCLUDE(x) #ifndef #x \
# #include<#x.h> \
# #endif
#
# and then i want to use INCLUDE(file1)
# INCLUDE(file2) ...etc.,

cpp barely qualifies as a macro processor. What you are trying
to do is not guarenteed to work, and rarely does.

In fact, it's guaranteed not to work. If it does work, then your C
preprocessor is broken.

(Have you decided to start snipping attributions as well as using an
obnoxious quoting character?)
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top