By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

Manipulating string constants with preprocessor macros - possible?

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.