preprocessor: macros in a string...

R

.rhavin grobert

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
#define MINORRELEASE 4
#endif

#define TXT_VERPRODUCT
"#MAJORRELEASE.#MINORRELEASE.#PATCHLEVEL "
#define TXT_VERFILE
"#MAJORRELEASE.#MINORRELEASE.#PATCHLEVEL "
#define TXT_NUM_VERFILE MAJORRELEASE,MINORRELEASE,PATCHLEVEL,0
#define TXT_NUM_VERPRODUCT MAJORRELEASE,MINORRELEASE,PATCHLEVEL,0

_______________________________
after preprocessing, TXT_VERPRODUCT is " #MAJORRELEASE .
#MINORRELEASE . #PATCHLEVEL "

..... how do i tell the preproc to change it to " 0 . 5 . 7 " ?!?


second question:

is there a way to have MINORRELEASE inc by one in case of #ifdef
_DEBUG ?

thanks for your thoughts, -.rhavin;)
 
W

Walter Roberson

i read that the preproc will parse macros inside a string if they are
prefixed with a sharp.

No, that is incorrect.
so i did....
#define MAJORRELEASE 0
#define PATCHLEVEL 7
#ifdef _DEBUG
#define MINORRELEASE 5
#else
#define MINORRELEASE 4
#endif
#define TXT_VERPRODUCT
"#MAJORRELEASE.#MINORRELEASE.#PATCHLEVEL "

Try

#define STRING(val) #VAL
#define TXT_VERPRODUCT STRING(MAJORRELEASE) "." STRING(MINORRELEASE) "." STRING(PATCHLEVEL) " "

The # signals the preprocessor to construct a literal string
"containing the spelling" of the value passed to it. So
TXT_VERPRODUCT would get replaced with six literal strings
(including the " " at the end in the six). Then you rely upon
another feature of the preprocessor, namely that adjacent
literal strings are merged into a single string.
 
P

Peter Nilsson

[email protected] (Walter Roberson) said:
No, that is incorrect.


Try

#define STRING(val) #VAL
^^^
s/val/VAL/

But that still won't quite work.
#define TXT_VERPRODUCT STRING(MAJORRELEASE) "." STRING \
(MINORRELEASE) "." STRING(PATCHLEVEL) " "

Better still, try...

#define MAJORRELEASE 0
#define PATCHLEVEL 7
#ifdef _DEBUG
#define MINORRELEASE 5
#else
#define MINORRELEASE 4
#endif

#define STRING(val) #val
#define STRSTR(mac) STRING(mac)

#define TXT_VERPRODUCT \
STRSTR(MAJORRELEASE) \
"." STRSTR(MINORRELEASE) \
"." STRSTR(PATCHLEVEL)

#include <stdio.h>

int main(void)
{
puts(TXT_VERPRODUCT);
return 0;
}
 
R

.rhavin grobert

Better still, try...
#define STRING(val) #val
#define STRSTR(mac) STRING(mac)

#define TXT_VERPRODUCT \
STRSTR(MAJORRELEASE) \
"." STRSTR(MINORRELEASE) \
"." STRSTR(PATCHLEVEL)

could you please explain why you need to double-substitute it?
 
E

Eric Sosman

..rhavin grobert said:
could you please explain why you need to double-substitute it?

Try it with just one level of substitution and see what
you get.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top