![]() |
#define macro to enclose an older macro with strings
Hey people, i'll try to keep this short ;)
Here is what I want to type (or at least close too)... #define VER_BUILD 1 #define STR_VER_BUILD "VER_BUILD" But what happends is the preprocessor see the quots in STR_VER_BUILD and replaces that text with "VER_BUILD"... I need it to see the VER_BUILD and replace it with 1, and only after doing that replacement enclose the 1 in quots... I tried using a number sign without any luck (number sign in a macro that takes params encloses the next param in quots). #define VER_BUILD 1 #define THING_TO_STR(thing) # thing #define STR_VER_BUILD THING_TO_STR(VER_BUILD) Both these tries gave me the result of STR_VER_BUILD being replaced with "VER_BUILD"... Instead of what I wanted... VER_BUILD being replaced with 1 and STR_VER_BUILD being replaced with "1" Just so there isn't any confusion... any other place were i used VER_BUILD alone, the preprocessor replaced that with 1. |
Re: #define macro to enclose an older macro with strings
"Dead RAM" <deadram@hotmail.com> wrote in message news:1oPIc.911822$Ar.646403@twister01.bloor.is.net .cable.rogers.com... > Hey people, i'll try to keep this short ;) > > Here is what I want to type (or at least close too)... > > #define VER_BUILD 1 > #define STR_VER_BUILD "VER_BUILD" > > But what happends is the preprocessor see the quots in STR_VER_BUILD and > replaces that text with "VER_BUILD"... > I need it to see the VER_BUILD and replace it with 1, and only after doing > that replacement enclose the 1 in quots... > I tried using a number sign without any luck (number sign in a macro that > takes params encloses the next param in quots). > > #define VER_BUILD 1 > #define THING_TO_STR(thing) # thing > #define STR_VER_BUILD THING_TO_STR(VER_BUILD) > > Both these tries gave me the result of STR_VER_BUILD being replaced with > "VER_BUILD"... Instead of what I wanted... VER_BUILD being replaced with 1 > and STR_VER_BUILD being replaced with "1" > This is a good example of the weirdness that is the C pre-processor. This minor variation works #define VER_BUILD 1 #define _THING_TO_STR(thing) # thing #define THING_TO_STR(thing) _THING_TO_STR(thing) #define STR_VER_BUILD THING_TO_STR(VER_BUILD) Don't ask me to explain why because I don't know. It just a trick worth knowing. john |
Re: #define macro to enclose an older macro with strings
>
> #define VER_BUILD 1 > #define _THING_TO_STR(thing) # thing > #define THING_TO_STR(thing) _THING_TO_STR(thing) > #define STR_VER_BUILD THING_TO_STR(VER_BUILD) > Before some else points it out, identifiers with a leading underscore followed by uppercase letter are _NOT_ALLOWED_. Replace _THING_TO_STR with something more suitable. john |
Re: #define macro to enclose an older macro with strings
Dead RAM wrote:
> Hey people, i'll try to keep this short ;) > > Here is what I want to type (or at least close too)... > > #define VER_BUILD 1 > #define STR_VER_BUILD "VER_BUILD" > > But what happends is the preprocessor see the quots in STR_VER_BUILD and > replaces that text with "VER_BUILD"... > I need it to see the VER_BUILD and replace it with 1, and only after doing > that replacement enclose the 1 in quots... > I tried using a number sign without any luck (number sign in a macro that > takes params encloses the next param in quots). > > #define VER_BUILD 1 > #define THING_TO_STR(thing) # thing > #define STR_VER_BUILD THING_TO_STR(VER_BUILD) > > Both these tries gave me the result of STR_VER_BUILD being replaced with > "VER_BUILD"... Instead of what I wanted... VER_BUILD being replaced with 1 > and STR_VER_BUILD being replaced with "1" > > Just so there isn't any confusion... any other place were i used VER_BUILD > alone, the preprocessor replaced that with 1. #include <stdio.h> #include <stdlib.h> #define STR_VER_BUILD "1" #define VER_BUILD atoi(STR_VER_BUILD) int main(void) { printf("%d = \"%s\"\n", VER_BUILD, STR_VER_BUILD); return 0; } |
Re: #define macro to enclose an older macro with strings
hack_tick wrote:
> > hi there > "John Harrison" <john_andronicus@hotmail.com> wrote in message > news:2li0ihFcpgj6U1@uni-berlin.de... > [..] > > Before some else points it out, identifiers with a leading underscore > > followed by uppercase letter are _NOT_ALLOWED_. Replace _THING_TO_STR with > > something more suitable. > > is it just the case with Macros or with All the identifiers ? is it Standard All identifiers and yes it is Standard. Names starting with an underscore followed by an uppercase letter are reserved for the compiler to aid in implementing its own macros and templates without interfering with user written source code. -- Karl Heinz Buchegger kbuchegg@gascad.at |
Re: #define macro to enclose an older macro with strings
hi there
"John Harrison" <john_andronicus@hotmail.com> wrote in message news:2li0ihFcpgj6U1@uni-berlin.de... [..] > Before some else points it out, identifiers with a leading underscore > followed by uppercase letter are _NOT_ALLOWED_. Replace _THING_TO_STR with > something more suitable. is it just the case with Macros or with All the identifiers ? is it Standard ? |
Re: #define macro to enclose an older macro with strings
BTW its working with VC6 for macros and identifiers(i tried with variable's
name only) |
Re: #define macro to enclose an older macro with strings
hack_tick wrote:
> > BTW its working with VC6 for macros and identifiers(i tried with variable's > name only) Sure it works. This rule is more on the level of an (enforced) agreement. There is nothing wrong with those names per se. But the compiler needs some names (for macros, templates, ...) on it's own (eg. open the iostream header file, if it exists on your implementation and see for yourself). That's why the standard reserves such names for exclusive use of compiler writers only. Just avoid such names and your macros will not interfere with something you get unexpected by including some system header files. -- Karl Heinz Buchegger kbuchegg@gascad.at |
Re: #define macro to enclose an older macro with strings
I don't know how... or why... but ummm... 0.o thanks...
As a side note... I hate the guy who built the pre-processor... but love the brain that caused this hack to show up ;) |
Re: #define macro to enclose an older macro with strings
Dead RAM posted:
> #define VER_BUILD 1 > #define THING_TO_STR(thing) # thing > #define STR_VER_BUILD THING_TO_STR(VER_BUILD) > > Both these tries gave me the result of STR_VER_BUILD being replaced > with "VER_BUILD"... Instead of what I wanted... VER_BUILD being > replaced with 1 and STR_VER_BUILD being replaced with "1" This in some perverse way actually pleases me. unsigned char const ver_build = 1; const char* const str_ver_build = "1"; -JKop |
| All times are GMT. The time now is 07:56 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.