473,386 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

# character in macros

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.

Jul 4 '07 #1
6 1658
On Jul 4, 11:44 am, Venky <venkatesh.duggir...@gmail.comwrote:
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.

Jul 4 '07 #2
Spade wrote, On 04/07/07 08:53:
On Jul 4, 11:44 am, Venky <venkatesh.duggir...@gmail.comwrote:
>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
<snip>
>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.
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.
--
Flash Gordon
Jul 4 '07 #3
Venky wrote:
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.
Jul 4 '07 #4
Venky wrote:
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.
Jul 4 '07 #5
# #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.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
One of the drawbacks of being a martyr is that you have to die.
Jul 4 '07 #6
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrites:
# #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?)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 4 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Chris Reedy | last post by:
For everyone - Apologies for the length of this message. If you don't want to look at the long example, you can skip to the end of the message. And for the Python gurus among you, if you can...
37
by: michele.simionato | last post by:
Paul Rubin wrote: > How about macros? Some pretty horrible things have been done in C > programs with the C preprocessor. But there's a movememnt afloat to > add hygienic macros to Python. Got any...
38
by: Haines Brown | last post by:
I'm having trouble finding the character entity for the French abbreviation for "number" (capital N followed by a small supercript o, period). My references are not listing it. Where would I...
7
by: Dave Ohlsson | last post by:
Hi, In ISO C/C++, a string constant prefixed by the letter `L' is a wide string constant and is of type "array of wchar_t". Consider the following C program fragment: #include <stdio.h>...
11
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 The program listed below demonstrates the use of wcsftime() and std::time_put<wchar_t> which is a C++ wrapper around it. (I know this isn't C; but...
21
by: aegis | last post by:
7.4#1 states The header <ctype.h> declares several functions useful for classifying and mapping characters.166) In all cases the argument is an int, the value of which shall be representable as an...
33
by: Robert Seacord | last post by:
When writing C99 code is a reasonable recommendation to use inline functions instead of macros? What sort of things is it still reasonable to do using macros? For example, is it reasonable to...
7
by: Jacek Dziedzic | last post by:
Hello! I am working on a code which features the following macros: #define myname \ ((std::string)(abi::__cxa_demangle(typeid(myself).name(),\ 0,0,&(Framework::demangle_status)))) #define...
15
by: prakash437 | last post by:
hello everyone, I have asked a similar question on the C++ group, however, i did not get the answer i was expecting ... How can i use a unsigned char array (of size, say 10 characters) as a set...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.