converting a preprocessor token to a string constant

J

John Devereux

Hi,

I would like to be able to define a number with the preprocessor, then
be able to use it to generate both numeric and string constants.

For example,

#define VERSION_MAJOR 4
#define VERSION_MINOR 47

#define VERSION_STRING "v" #VERSION_MAJOR "." #VERSION_MINOR

The above does not work but hopefully illustrates what I would like to
do, i.e. make VERSION_STRING end up as "v4.47".

Of course I realise I can use runtime C code to build strings
containing what I want, but I am thinking there must be a way to do it
with the preprocessor.

Thanks,
 
W

Walter Roberson

I would like to be able to define a number with the preprocessor, then
be able to use it to generate both numeric and string constants.
For example,
#define VERSION_MAJOR 4
#define VERSION_MINOR 47
#define VERSION_STRING "v" #VERSION_MAJOR "." #VERSION_MINOR
The above does not work but hopefully illustrates what I would like to
do, i.e. make VERSION_STRING end up as "v4.47".

#define VERSION_STRING_aux(major,minor) "v" #major "." #minor
#define VERSION_STRING VERSION_STRING_aux(VERSION_MAJOR,VERSION_MINOR)

The trick here is that # only works on macro arguments.
 
T

Thad Smith

Walter said:
#define VERSION_STRING_aux(major,minor) "v" #major "." #minor
#define VERSION_STRING VERSION_STRING_aux(VERSION_MAJOR,VERSION_MINOR)

The trick here is that # only works on macro arguments.

The other trick is that # works on the actual macro parameter. To force
a substitution of macro value, you need to do another level of macro
invocation. The version above generates the string
"vVERSION_MAJOR.VERSION_MINOR".

To generate the proper numeric form use something like

#define VERSION_MAJOR 4
#define VERSION_MINOR 47

#define STRINGIZE2(s) #s
#define STRINGIZE(s) STRINGIZE2(s)
#define VERSION_STRING "v" STRINGIZE(VERSION_MAJOR) \
"." STRINGIZE(VERSION_MINOR)

#include <stdio.h>
int main() {
printf ("%s\n", VERSION_STRING);
return 0;
}
 
J

John Devereux

Thad Smith said:
The other trick is that # works on the actual macro parameter. To
force a substitution of macro value, you need to do another level of
macro invocation. The version above generates the string
"vVERSION_MAJOR.VERSION_MINOR".

Indeed, I had already tried that before posting and that is precisely
what happened!
To generate the proper numeric form use something like

#define VERSION_MAJOR 4
#define VERSION_MINOR 47

#define STRINGIZE2(s) #s
#define STRINGIZE(s) STRINGIZE2(s)
#define VERSION_STRING "v" STRINGIZE(VERSION_MAJOR) \
"." STRINGIZE(VERSION_MINOR)

#include <stdio.h>
int main() {
printf ("%s\n", VERSION_STRING);
return 0;
}

That works. I knew there had to be a way...

Thanks!
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top