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

preprocessor: macros in a string...

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

Mar 22 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In article <11*********************@d57g2000hsg.googlegroups. com>,
..rhavin grobert <cl***@yahoo.dewrote:
>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.
--
Programming is what happens while you're busy making other plans.
Mar 22 '07 #2

P: n/a
rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:
rhavin grobert <c...@yahoo.dewrote:
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
^^^
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;
}

--
Peter

Mar 22 '07 #3

P: n/a
On Mar 22, 9:57 pm, "Peter Nilsson" <a...@acay.com.auwrote:
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?
Mar 23 '07 #4

P: n/a
..rhavin grobert wrote:
On Mar 22, 9:57 pm, "Peter Nilsson" <a...@acay.com.auwrote:
>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
es*****@acm-dot-org.invalid
Mar 23 '07 #5

P: n/a
On Mar 23, 10:01 am, ".rhavin grobert" <c...@yahoo.dewrote:
On Mar 22, 9:57 pm, "Peter Nilsson" <a...@acay.com.auwrote:
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?
This question, along with your original one, are explained in the FAQ
(http://c-faq.com/) in question 11.17.

Robert Gamble

Mar 23 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.