Connecting Tech Pros Worldwide Help | Site Map

preprocessor: macros in a string...

.rhavin grobert
Guest
 
Posts: n/a
#1: Mar 22 '07
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;)

Walter Roberson
Guest
 
Posts: n/a
#2: Mar 22 '07

re: preprocessor: macros in a string...


In article <1174596023.581460.11140@d57g2000hsg.googlegroups. com>,
..rhavin grobert <clqrq@yahoo.dewrote:
Quote:
>i read that the preproc will parse macros inside a string if they are
>prefixed with a sharp.
No, that is incorrect.
Quote:
>so i did....
Quote:
>#define MAJORRELEASE 0
>#define PATCHLEVEL 7
Quote:
>#ifdef _DEBUG
#define MINORRELEASE 5
>#else
#define MINORRELEASE 4
>#endif
Quote:
>#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.
--
Programming is what happens while you're busy making other plans.
Peter Nilsson
Guest
 
Posts: n/a
#3: Mar 22 '07

re: preprocessor: macros in a string...


rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:
Quote:
rhavin grobert <c...@yahoo.dewrote:
Quote:
i read that the preproc will parse macros inside a string if
they are prefixed with a sharp.
>
No, that is incorrect.
>
Quote:
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
^^^
s/val/VAL/

But that still won't quite work.
Quote:
#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;
}

--
Peter

.rhavin grobert
Guest
 
Posts: n/a
#4: Mar 23 '07

re: preprocessor: macros in a string...


On Mar 22, 9:57 pm, "Peter Nilsson" <a...@acay.com.auwrote:
Quote:
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?


Eric Sosman
Guest
 
Posts: n/a
#5: Mar 23 '07

re: preprocessor: macros in a string...


..rhavin grobert wrote:
Quote:
On Mar 22, 9:57 pm, "Peter Nilsson" <a...@acay.com.auwrote:
>
Quote:
>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?
Try it with just one level of substitution and see what
you get.

--
Eric Sosman
esosman@acm-dot-org.invalid
Robert Gamble
Guest
 
Posts: n/a
#6: Mar 23 '07

re: preprocessor: macros in a string...


On Mar 23, 10:01 am, ".rhavin grobert" <c...@yahoo.dewrote:
Quote:
On Mar 22, 9:57 pm, "Peter Nilsson" <a...@acay.com.auwrote:
>
Quote:
Better still, try...
#define STRING(val) #val
#define STRSTR(mac) STRING(mac)
>
Quote:
#define TXT_VERPRODUCT \
STRSTR(MAJORRELEASE) \
"." STRSTR(MINORRELEASE) \
"." STRSTR(PATCHLEVEL)
>
could you please explain why you need to double-substitute it?
This question, along with your original one, are explained in the FAQ
(http://c-faq.com/) in question 11.17.

Robert Gamble

Closed Thread