473,670 Members | 2,495 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Manipulating string constants with preprocessor macros - possible?

I wrote a program that handles the buildcount of projects (gets called
every time I compile a project, it writes a header file which is
#include-ed in the project).
My question is this, is it possible to modify string constants with
preprocessor macros? for now I write every string I need as a #define
in the generated header, when in theory I only need to define the
buildcount there and put it then in the places where I need it.

for instance:

#define BUILD 1
somewhere in the program I have string constant

char *version = "XYZ Version 2.3.21 Build X";

now I want to replace the X with the actual buildcount, but macros
inside quotationmarks don't work. Is there a way to somehow put the
quotationmarks around the string constant after the BUILD macro was
applied? With a macro?

I hope I made my problem clear, TIA :-)

Veit

--
if life throws you lemons make lemonade
Nov 13 '05 #1
5 13749
Veit Wiessner wrote:
I wrote a program that handles the buildcount of projects (gets called
every time I compile a project, it writes a header file which is
#include-ed in the project).
My question is this, is it possible to modify string constants with
preprocessor macros? for now I write every string I need as a #define
in the generated header, when in theory I only need to define the
buildcount there and put it then in the places where I need it.

for instance:

#define BUILD 1
somewhere in the program I have string constant

char *version = "XYZ Version 2.3.21 Build X";

now I want to replace the X with the actual buildcount, but macros
inside quotationmarks don't work. Is there a way to somehow put the
quotationmarks around the string constant after the BUILD macro was
applied? With a macro?

#define STRINGIFY(x) #x
#define BUILD 1

char *version = "XYZ Version 2.3.21 Build " STRINGIFY(BUILD );

int main(void) {
printf("Build is %s\n", version);
return 0;
}

HTH,
-nrk.
I hope I made my problem clear, TIA :-)

Veit


Nov 13 '05 #2
nrk
ram_nrk200 wrote:
Veit Wiessner wrote:
I wrote a program that handles the buildcount of projects (gets called
every time I compile a project, it writes a header file which is
#include-ed in the project).
My question is this, is it possible to modify string constants with
preprocessor macros? for now I write every string I need as a #define
in the generated header, when in theory I only need to define the
buildcount there and put it then in the places where I need it.

for instance:

#define BUILD 1
somewhere in the program I have string constant

char *version = "XYZ Version 2.3.21 Build X";

now I want to replace the X with the actual buildcount, but macros
inside quotationmarks don't work. Is there a way to somehow put the
quotationmarks around the string constant after the BUILD macro was
applied? With a macro?


#define STRINGIFY(x) #x
#define BUILD 1

char *version = "XYZ Version 2.3.21 Build " STRINGIFY(BUILD );

int main(void) {
printf("Build is %s\n", version);
return 0;
}

HTH,
-nrk.


Apologies for the crap above, should've run the darn thing before posting.
Here, try this one... it should work:

#define STRINGIFY(x) #x
#define EXPAND(x) STRINGIFY(x)
#define BUILD 1

char *version = "XYZ Version 2.3.21 Build " EXPAND(BUILD);

int main(void) {
printf("Build is %s\n", version);
return 0;
}

-nrk.
Nov 13 '05 #3
nrk wrote:

Apologies for the crap above, should've run the darn thing before
posting. Here, try this one... it should work:

#define STRINGIFY(x) #x
#define EXPAND(x) STRINGIFY(x)
#define BUILD 1

char *version = "XYZ Version 2.3.21 Build " EXPAND(BUILD);

int main(void) {
printf("Build is %s\n", version);
return 0;
}

-nrk.


Exactly what I wanted, thank you very much!

Veit

--
if life throws you lemons make lemonade
Nov 13 '05 #4
I am interested in knowing how you are incrementing the build count.
Are you doing it manually or by making use of a 'makefile' that does
this? and if so how?

TIA,
john

In article <bq************ *@ID-95951.news.uni-berlin.de>, Veit Wiessner wrote:
nrk wrote:

Apologies for the crap above, should've run the darn thing before
posting. Here, try this one... it should work:

#define STRINGIFY(x) #x
#define EXPAND(x) STRINGIFY(x)
#define BUILD 1

char *version = "XYZ Version 2.3.21 Build " EXPAND(BUILD);

int main(void) {
printf("Build is %s\n", version);
return 0;
}

-nrk.


Exactly what I wanted, thank you very much!

Veit

Nov 13 '05 #5
John Navil Joseph wrote:
I am interested in knowing how you are incrementing the build count.
Are you doing it manually or by making use of a 'makefile' that does
this? and if so how?


I do it in the makefile, I added a 'buildcount' target (which only
calls the buildcount program itself) and the obj-targets which include
the generated header depend on it.
Veit

--
if life throws you lemons make lemonade
Nov 13 '05 #6

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

Similar topics

25
9116
by: Sabyasachi Basu | last post by:
While trying to port some stuff from Unix to Windows, I encountered a strange behaviour of function macros with empty arguments. Here is a small snippet which illustrates the problem: #include <iostream> #include <string> using namespace std; #define B(X, Y) Y
13
1875
by: Dave | last post by:
Can anybody tell me why the code below should produce the error below? I'm going nuts here! Am I missing something incredibly obvious and simple? std::string::size_type idx; std::string one_line; .. .. .. idx = one_line.find('\t', 0);
4
1981
by: William Payne | last post by:
Hello, I am starting to steer away from the practice of using "using namespace std;" in my code. Instead I am qualifying each name in the source when I use them, for example: std::cout << "Hello"; Now to my question. Depending upon the status of my program, I return either EXIT_SUCCESS or EXIT_FAILURE from main(). Thinking that these constants live in the std namespace, I tried: return std::EXIT_FAILURE; but my compiler said:...
97
27773
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
205
10597
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
10
4229
by: tvn007 | last post by:
#include <iostream> #include <string> int main (void){ using namespace std; string STR =("TEST"); const std::string::size_type STR_SIZE = STR.size(); int count =0; while ( count <= STR_SIZE){
4
1947
by: Jim Ford | last post by:
I have a single C file with the following code: int f2() { /* Blah-blah */ } int f1() { /* Blah-blah */
3
9522
by: Kevin Frey | last post by:
I am porting Managed C++ code from VS2003 to VS2005. Therefore adopting the new C++/CLI syntax rather than /clr:oldSyntax. Much of our managed code is concerned with interfacing to native C++ code (ie. wrappers etc). In Managed C++ there was an automatic conversion between const char* and String^. This was useful to us for two reasons: 1. We declared our string constants as eg. const char* const c_My_Constant = "blah", and these...
5
2535
by: .rhavin grobert | last post by:
i read that the preproc will parse macros inside a string if they are prefixed with a sharp. so i did.... ________________________- #define MAJORRELEASE 0 #define PATCHLEVEL 7 #ifdef _DEBUG #define MINORRELEASE 5 #else
0
8384
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8896
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8590
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8659
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7410
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6211
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4387
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2798
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1790
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.