#if CONSTANT == 1 checking problem

T

Timo

I am having a problem with some preprocessor constant value checking:

header.h:

#define MY_CONSTANT 1

file.c

#if MY_CONSTANTTT == 1 // <- note the typo!!
// dostuff...
#endif

I have always thought that C-compiler should give error message of
that, because the constant name "MY_CONSTANTTT" doesn't exist.
However,
I got no error message with the line (Tested with MS Visual C++ 6.0
and
with one emdedded system compiler, no other compilers available now).

What I am trying to achieve is to define some product features which
should be included to compilation, in common header file. Usually this
is done with #define USE_FEATURE_XXX. But if programmer writes
"USE_FEATURE_XXX" incorrectly in C source, the code is not
included in compilation, but no error message is got. Example:

#ifdef USE_FEATURE_X // <-typo
// do something
#endif

I am trying to catch possible typing error with this "#if
USE_FEATURE_XXX == 1"
syntax, but it seems not to be working. Have I misunderstood C
standard, or are the compilers I use non-standard?

BR,
Timo
 
J

Joona I Palaste

Timo said:
I am having a problem with some preprocessor constant value checking:

#define MY_CONSTANT 1

#if MY_CONSTANTTT == 1 // <- note the typo!!
// dostuff...
#endif
I have always thought that C-compiler should give error message of
that, because the constant name "MY_CONSTANTTT" doesn't exist.
However,
I got no error message with the line (Tested with MS Visual C++ 6.0
and
with one emdedded system compiler, no other compilers available now).

Yes, this is perfectly standard. The #if directive considers
nonexistent macro names as equal to 0, AFAIK.
What I am trying to achieve is to define some product features which
should be included to compilation, in common header file. Usually this
is done with #define USE_FEATURE_XXX. But if programmer writes
"USE_FEATURE_XXX" incorrectly in C source, the code is not
included in compilation, but no error message is got. Example:
#ifdef USE_FEATURE_X // <-typo
// do something
#endif
I am trying to catch possible typing error with this "#if
USE_FEATURE_XXX == 1"
syntax, but it seems not to be working. Have I misunderstood C
standard, or are the compilers I use non-standard?

You have misunderstood C standard. I don't know (yet) of any way of
doing what you want, but with some preprocessing trickery it might be
possible.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"C++ looks like line noise."
- Fred L. Baube III
 
J

Jeremy Yallop

Timo said:
#define MY_CONSTANT 1

file.c

#if MY_CONSTANTTT == 1 // <- note the typo!!
// dostuff...
#endif

I have always thought that C-compiler should give error message of
that, because the constant name "MY_CONSTANTTT" doesn't exist.

Undefined identifiers in an "#if" directive are replaced with "0".

Jeremy.
 
F

Fred L. Kleinschmidt

Timo said:
I am having a problem with some preprocessor constant value checking:

header.h:

#define MY_CONSTANT 1

file.c

#if MY_CONSTANTTT == 1 // ?- note the typo!!
// dostuff...
#endif

I have always thought that C-compiler should give error message of
that, because the constant name "MY_CONSTANTTT" doesn't exist.
However,
I got no error message with the line (Tested with MS Visual C++ 6.0
and
with one emdedded system compiler, no other compilers available now).

What I am trying to achieve is to define some product features which
should be included to compilation, in common header file. Usually this
is done with #define USE_FEATURE_XXX. But if programmer writes
"USE_FEATURE_XXX" incorrectly in C source, the code is not
included in compilation, but no error message is got. Example:

#ifdef USE_FEATURE_X // ?-typo
// do something
#endif

I am trying to catch possible typing error with this "#if
USE_FEATURE_XXX == 1"
syntax, but it seems not to be working. Have I misunderstood C
standard, or are the compilers I use non-standard?

BR,
Timo

The precompiler does not distinguiush between symbols defined in the
current file (or any #include'd file) and defined environment variables.
Thus there is no way for it to know that you have made a typo.

For example, if you typed "export MY_CONSTANTTT=1" in the command line,
then compiled your program, the block delimited by "#if MY_CONSTANTTT ==
1 " would be compiled.
 
A

Arthur J. O'Dwyer

The precompiler does not distinguiush between symbols defined in the
current file (or any #include'd file) and defined environment variables.
Thus there is no way for it to know that you have made a typo.

For example, if you typed "export MY_CONSTANTTT=1" in the command line,
then compiled your program, the block delimited by "#if MY_CONSTANTTT ==
1 " would be compiled.

That's one of the more bizarre incorrect explanations I've heard
here in a while. At first, I didn't even recognize the context;
I thought Fred was giving a Lisp-environment answer in the wrong
newsgroup or something. (Then a few seconds later I realized he
was probably using a proprietary compiler on a (proprietary?)
*nix system with 'export' being an OS shell command.)

'export' is not a feature of the C language.

Hoo, dear.

-Arthur
 
N

nrk

Arthur said:
That's one of the more bizarre incorrect explanations I've heard
here in a while. At first, I didn't even recognize the context;
I thought Fred was giving a Lisp-environment answer in the wrong
newsgroup or something. (Then a few seconds later I realized he
was probably using a proprietary compiler on a (proprietary?)
*nix system with 'export' being an OS shell command.)

'export' is not a feature of the C language.

Not only is it bizzare... It would probably drive me crazy (perhaps, because
I've never used such a weird system ever). Talk about compiler messages
that make you lose your hair fast, such an environment would just be loaded
with them. As a matter of self-defense, I ask the OP to reveal the system
under consideration, so I can keep a safe distance.

-nrk.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top