473,320 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

preprocessor: macros in a string...

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
5 2517
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
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
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
..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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
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...
4
by: Jakob Simon-Gaarde | last post by:
Some project includes files from different libraries lib1,lib2 and lib3 all having each there own version header file. I need to be able to pick up these values in a single define value...
205
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
4
by: Jim Ford | last post by:
I have a single C file with the following code: int f2() { /* Blah-blah */ } int f1() { /* Blah-blah */
13
by: seemanta dutta | last post by:
Greetings C gurus, I have used preprocessor directives since a very long time. But whenever I see some professional piece of C code, the linux kernel for example, I get literally confused by the...
5
by: Veit Wiessner | last post by:
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...
6
by: max(01)* | last post by:
hi. i want to examine preprocessed source which only has certain macros expanded, for example i would like to have: #include <stdio.h> #include "other.c" int main() { ... }
2
by: Prashant Mahajan | last post by:
I just wanted comments from you all on the following topic: Let's say we have 2, C code files namely file1.c and file2.c. file1.c contains few pre-processor definations say #define TEST1 10 We...
25
by: Gernot Frisch | last post by:
Hi, I want to build a encryption macro set, that can crypt: char secret = CRYPT("KungFu"); to anything unreadable, and then have a function: char* DECRYPT(char* str) { ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.