473,657 Members | 2,993 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7519
In article <87************ @cordelia.dever eux.me.uk>,
John Devereux <jd******@THISd evereux.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_MAJ OR,VERSION_MINO R)

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.dever eux.me.uk>,
John Devereux <jd******@THISd evereux.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_MAJ OR,VERSION_MINO R)

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(VERSI ON_MAJOR) \
"." STRINGIZE(VERSI ON_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.dever eux.me.uk>,
John Devereux <jd******@THISd evereux.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_MAJ OR,VERSION_MINO R)
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(VERSI ON_MAJOR) \
"." STRINGIZE(VERSI ON_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
10591
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
2338
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 / need this: .....somefunction("#225").... This string in the function call is the same number like in the define, but the constant wasn't used. Because for future changes and for readability I
24
40182
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 looking to write something along the lines of:
3
3138
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 have C code that self-diagnoses its configuration with, e.g., #define TKN2YESNO(x) ((x)==0 ? ("No"):("Yes"))
13
2119
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 defined = 2;
3
25999
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
3651
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. Could someone point me to the appropriate section, or else confirm the behaviour as a bug? For a particular project, I am using the C preprocessor phase only. I am not using the standalone program 'cpp' because proper functioning of my project...
12
2680
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 say. i got a couple of questions though: 1- is there any equivalent to java's Stream classes(ByteArrayInputStream,InputStreamRedare,DataInputStream ..) 2- how to open a stream on a byte ( char) array in c++ in order to read it as text. i have a...
10
2136
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 identifier ID i use #ID. Thats ok, but what if i want to alter it a little. like if i have an identifier myID , and i want to generate an string MYID (or any other form) from it.
0
8384
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8302
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8718
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8601
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.