473,398 Members | 2,380 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,398 software developers and data experts.

converting a preprocessor token to a string constant

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,

--

John Devereux
Nov 15 '05 #1
3 7507
In article <87************@cordelia.devereux.me.uk>,
John Devereux <jd******@THISdevereux.me.uk> wrote:
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.
--
Chocolate is "more than a food but less than a drug" -- RJ Huxtable
Nov 15 '05 #2
Walter Roberson wrote:
In article <87************@cordelia.devereux.me.uk>,
John Devereux <jd******@THISdevereux.me.uk> wrote:
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.


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;
}

Nov 15 '05 #3
Thad Smith <Th*******@acm.org> writes:
Walter Roberson wrote:
In article <87************@cordelia.devereux.me.uk>,
John Devereux <jd******@THISdevereux.me.uk> wrote:
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.


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!

--

John Devereux
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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
21
by: Eric | last post by:
Hello all, I've got this line of given code (cannot change this; wizard-generated, but value may change someday): #define IDB_BUTTON 225 Somewhere in the code I found /...
24
by: Nudge | last post by:
I have an array, and an unrolled loop which looks like this: do_something(A); do_something(A); .... do_something(A); I thought: why should I type so much? I should write a macro. So I was...
3
by: Charlie Zender | last post by:
Hi, I want to have a CPP macro that tests the value of a token and returns the string "No" if the token is undefined (or 0) and returns "Yes" if the token is defined (non-zero). Then I can...
13
by: Chris Croughton | last post by:
Is the following code standard-compliant, and if so what should it do? And where in the standard defines the behaviour? #include <stdio.h> #define DEF defined XXX int main(void) { int...
3
by: michael.martin | last post by:
Hi, I am wondering if something like this is possible? #define COLON ":" #define PERIOD "." #define WORD "word" #define WORD_COLON WORD+COLON
9
by: Walter Roberson | last post by:
I have run into a peculiarity with SGI's C compiler (7.3.1.2m). I have been reading carefully over the ANSI X3.159-1989 specification, but I cannot seem to find a justification for the behaviour....
12
by: steven acer | last post by:
hello, i have a java app that constructs an xml from a specific file format and vice versa. i've been asked to convert it to c++, but im not an expert in c++, actually im mere beginner you can...
10
by: abir | last post by:
Hi, This is not strictly a C++ language question. Is there any way to form a string from an identifier with the preprocessor with some operations ? i.e to say to make a string from an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.