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 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
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;
}
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
|
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 /...
|
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...
|
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...
|
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...
|
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
|
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....
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |