473,549 Members | 2,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String comparison in preprocessor commands

L.S.

How can I make certain code parts be compiled conditionally, depending
on the definition of a macro such as:

#define VERSION "2.3"

Is it all right to do things like:
#if VERSION == "2.3"
...../* conditionally compiled code */
#endif

#if VERSION != "2.3"
...../* conditionally compiled code */
#endif

#if VERSION > "2.3"
...../* conditionally compiled code */
#endif
Or should I go about this differently? Please note that I'm not in the
position to change the macro definition.
Thanks for any help,

Erik Leunissen
--
leunissen@ nl | Merge the left part of these two lines into one,
e. hccnet. | respecting a character's position in a line.

Nov 14 '05 #1
7 44349
Erik Leunissen wrote:
L.S.

How can I make certain code parts be compiled conditionally, depending
on the definition of a macro such as:

#define VERSION "2.3"

Is it all right to do things like:
#if VERSION == "2.3"
..../* conditionally compiled code */
#endif

#if VERSION != "2.3"
..../* conditionally compiled code */
#endif

#if VERSION > "2.3"
..../* conditionally compiled code */
#endif
Or should I go about this differently? Please note that I'm not in the
position to change the macro definition.
Please check the FAQ (http://www.eskimo.com/~scs/C-faq/top.html) before
posting, see question 10.12.

Thanks for any help,

Erik Leunissen


Rob Gamble

Nov 14 '05 #2
In article <42************ *********@reade r10.nntp.hccnet .nl>,
Erik Leunissen <lo**@the.foote r.invalid> wrote:


#if VERSION > "2.3"
..../* conditionally compiled code */
#endif


There is no simple, direct way to do this with macros.

Some unpleasant but workable solutions are:

- Rewrite the macro as separate integers. Then your conditional
compilation can do stuff like:

#if MAJOR_VERSION > 2 || (MAJOR_VERSION == 2 && MINOR_VERSION > 3) /*etc*/

- Rewrite the macro as a single integer with an implied decimal
point, i.e. if you think you'll never have a version between
2.99 and 3.00, you can do this:

#define VERSION 203

and then integer comparisons like

#if VERSION > 203

will do the right thing.

- Use an additional make dependency or a "pre-build" step to run a
small command line program that does something along the lines of:

int major, minor;
sscanf(VERSION, "%d.%d", &major, &minor); /* not the best way */
printf("#define MAJOR_VERSION %d\n", major);
printf("#define MINOR_VERSION %d\n", minor);
printf("#define IMPLIED_DECIMAL _VERSION %d\n", major*100+minor );

with output redirected to a .h file. Then you can use one of
the previous solutions w/o changing VERSION itself.

- If the difference between versions is something like a significant
API change, it's often the case that the API has some new macros or
has removed some old ones. If you can make the conditional
compliation depend on the presense/absence of a guaranteed part of
the API then you don't need to specifically use the version number
itself.

- If the conditionally compiled code is not a huge amount, make it not
be conditionally compiled--do the check for version number at
run time. Convert to double and plain old if's will work. Unless
you have some hard space or time performance limits, it's likely
that the extra overhead will be unnoticeable. Also, this opens up
the possibility of using something like an environment variable
to switch between old and new behaviors.
--
7842++
Nov 14 '05 #3


Erik Leunissen wrote:
L.S.

How can I make certain code parts be compiled conditionally, depending
on the definition of a macro such as:

#define VERSION "2.3"

Is it all right to do things like:
#if VERSION == "2.3"
..../* conditionally compiled code */
#endif

#if VERSION != "2.3"
..../* conditionally compiled code */
#endif

#if VERSION > "2.3"
..../* conditionally compiled code */
#endif
No, that won't work: The preprocessor can generate
string literals, but it can't actually work with strings.
(In particular, it can't compare them.)

What you *can* do, which may be satisfactory for some
purposes, is use the preprocessor to generate a compile-
time constant, test that value with `if' instead of `#if',
and rely on the compiler to eliminate dead code:

#define MAJOR (VERSION[0] - '0')
#define MINOR (VERSION[2] - '0')
if (MAJOR == 2 && MINOR == 3) { ... }
if (MAJOR != 2 || MINOR != 3) { ... }
if (MAJOR > 2 || (MAJOR == 2 && MINOR > 3)) { ... }

However, this trick will only work for executable statements;
you can't use it to "conditiona lly compile" declarations and
the like. Also, it will break pretty badly if VERSION ever
becomes "2.10" or "10.0" ...
Or should I go about this differently? Please note that I'm not in the
position to change the macro definition.


If you really cannot change the macro definition, things
are going to be messy. The best I can suggest is to use a
"helper" program as suggested by Anonymous 7843. (However,
note that his suggestion of converting VERSION to `double'
is not very robust; consider the "2.10" case. The '.' in
a version number is a field separator, not a decimal point.)

If you must leave VERSION's string-ness and value intact
but are allowed to change the way it's defined, things can
be lots easier. Instead of trying to extract MAJOR and MINOR
from VERSION, you could use MAJOR and MINOR as the "primary
sources" and derive VERSION from them:

#define MAJOR 2
#define MINOR 3

#define STRING(x) STR_HELPER(x)
#define STR_HELPER(x) #x
#define VERSION STRING(MAJOR) "." STRING(MINOR)

This gives VERSION the same value as before, but makes MAJOR
and MINOR available for straightforward preprocessor tests.

--
Er*********@sun .com

Nov 14 '05 #4
Thanks all for your valuable insight and suggestions; they've helped a lot.

Greetings,

Erik Leunissen
==============
Erik Leunissen wrote:
L.S.

How can I make certain code parts be compiled conditionally, depending
on the definition of a macro such as:

#define VERSION "2.3"

Is it all right to do things like:
#if VERSION == "2.3"
..../* conditionally compiled code */
#endif

#if VERSION != "2.3"
..../* conditionally compiled code */
#endif

#if VERSION > "2.3"
..../* conditionally compiled code */
#endif
Or should I go about this differently? Please note that I'm not in the
position to change the macro definition.
Thanks for any help,

Erik Leunissen


--
leunissen@ nl | Merge the left part of these two lines into one,
e. hccnet. | respecting a character's position in a line.

Nov 14 '05 #5
Erik Leunissen wrote:

L.S.

How can I make certain code parts be compiled conditionally, depending
on the definition of a macro such as:

#define VERSION "2.3"

Is it all right to do things like:

#if VERSION == "2.3"
..../* conditionally compiled code */
#endif [...] Or should I go about this differently? Please note that I'm not in the
position to change the macro definition.


Don't define a string... define a number.

#define VERSION 0x0203

To test for that version:

#if VERSION == 0x0203

To test for prior to that version:

#if VERSION < 0x0203

To test for that version or later

#if VERSION >= 0x0203

And so on.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Nov 14 '05 #6
In article <42************ ***@spamcop.net >,
Kenneth Brody <ke******@spamc op.net> wrote:
....
Or should I go about this differently? Please note that I'm not in the
position to change the macro definition.


Don't define a string... define a number.


What part of "I'm not in the position to change the macro definition" are
you having a problem with?

Nov 14 '05 #7
Kenny McCormack wrote:

In article <42************ ***@spamcop.net >,
Kenneth Brody <ke******@spamc op.net> wrote:
...
Or should I go about this differently? Please note that I'm not in the
position to change the macro definition.


Don't define a string... define a number.


What part of "I'm not in the position to change the macro definition" are
you having a problem with?


The part that my brain didn't process while reading it.

D'oh!

Is he in a position to define an additional macro along side this one?

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Nov 14 '05 #8

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

Similar topics

0
1483
by: ramin | last post by:
Hi, I would be greately thankful if somebody can give me some information about how string comparison is implemented in mysql. (Both in Physical and Application layer) How queries on string (for example '>' or like or strcmp) are implemented. Any idea where can I find the open source for string comparison/ ordering/ implementation? Does it...
46
5109
by: yadurajj | last post by:
Hello i am newbie trying to learn C..I need to know about string comparisons in C, without using a library function,...recently I was asked this in an interview..I can write a small program but I was told that wouldn't it be wise to first get the length of the strings..if it doesn't match then they are not the same..I agreed...then he...
4
7170
by: Peter Kirk | last post by:
Hi I am looking at some code which in many places performs string comparison using == instead of Equals. Am I right in assuming that this will in fact work "as expected" when it is strings involved? By "as expected" I mean that as long as the strings are instantiated using string literals, then == and Equals are the same. string s1 =...
1
1558
by: bjjnova | last post by:
I have the following string comparison that is throwing an error I cannot find ( I will include my attempts to trace the error) In the line following the asterisks, written as I have below, a true is always returned and the printed msg appears that the 2 variables are not equal. This happens even when the print statement writes out,...
4
3479
by: almurph | last post by:
Hi, Hope you can help me with this one. I'm looking for some nice string comparison algorithms. I want to be able to compare 2 strings (fairly smallish, less than 50 characters) and return a % of how well they are similar. So, 2 strings that are absoloutly identical will return 100%. Strings that are radically different will return...
9
3745
by: Usman Jamil | last post by:
Hi I'm having a strange error while comparing two strings. Please check the code below. This is a simple string comparison code and works just fine on all of my machines. While debugging an issue on a client's machine, who had turkish windows installed on his system, I found out that this simple piece of code does'nt work. The messages...
14
13465
by: Steve Bergman | last post by:
I'm looking for a module to do fuzzy comparison of strings. I have 2 item master files which are supposed to be identical, but they have thousands of records where the item numbers don't match in various ways. One might include a '-' or have leading zeros, or have a single character missing, or a zero that is typed as a letter 'O'. That...
3
5047
by: questionit | last post by:
Hi I have a string comparison code from VB. While (Not ((myString Like "??????") And (myString Like "00*"))) All i am wondering is if we can do the similar thing in C/C++ i.e using 'Like' keyword and doing wildcards like in above example?
3
4078
by: itmfl | last post by:
We are writing a program that multiplies two matrices of size n x m and m x n together. The matrices are stored in a file. The user provides the filename in the command line prompt. The file is formatted like so: /beginning/ matrix1 3 2 1 6 2 4 3 5 matrix2
10
8512
by: lilly07 | last post by:
Hi, I have one column of strings in 1st file file and another file which consists of 5 clumns in each line and my basic objective is to find each item/line of 1st file is available in 3rd column of 2 nd file. And I tried the following logic. It might be bit round about way but as a beginner am trying as follows. The column in the 1st file...
0
7451
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...
0
7720
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7959
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...
1
7473
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7810
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...
1
5369
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...
0
3501
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...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
764
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...

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.