String comparison in preprocessor commands

E

Erik Leunissen

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
 
T

tigervamp

Erik said:
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
 
A

Anonymous 7843

#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.
 
E

Eric Sosman

Erik said:
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 "conditionally 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.
 
E

Erik Leunissen

Thanks all for your valuable insight and suggestions; they've helped a lot.

Greetings,

Erik Leunissen
==============
 
K

Kenneth Brody

Erik said:
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:[email protected]>
 
K

Kenny McCormack

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.[/QUOTE]

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

Kenneth Brody

Kenny said:
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?[/QUOTE]

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:[email protected]>
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top