473,395 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

#if CONSTANT == 1 checking problem

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
Nov 13 '05 #1
5 1785
Timo <t_**********@yahoo.com> scribbled the following:
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).
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 (pa*****@cc.helsinki.fi) ---------------------------\
| 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
Nov 13 '05 #2
Timo wrote:
#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.
Nov 13 '05 #3


Timo wrote:

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.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
Nov 13 '05 #4

On Fri, 3 Oct 2003, Fred L. Kleinschmidt wrote:

Timo wrote:

#if MY_CONSTANTTT == 1 // ?- note the typo!!
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.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow


Hoo, dear.

-Arthur

Nov 13 '05 #5
nrk
Arthur J. O'Dwyer wrote:

On Fri, 3 Oct 2003, Fred L. Kleinschmidt wrote:

Timo wrote:
>
> #if MY_CONSTANTTT == 1 // ?- note the typo!!

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.


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.

Nov 13 '05 #6

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

Similar topics

2
by: Piotr Bartczak | last post by:
I've started learning C# (after C++) and I found that there is no way to make an object constant. In C++ there is a const modifier which allows me to define an object which will not change after...
2
by: David Green | last post by:
Ok, i'm a real c n00b but i needed a piece of code for some work i was doing. Initially i was running the stuff under linux and using gcc to compile the c code and it worked fine but now i need to...
14
by: Urs Thuermann | last post by:
What is the most elegant way to check certain conditions at compile time? I.e. I want a compile time error to be generated if for example the size of a struct is not a multiple of 4 or if one...
6
by: niklaus | last post by:
Hi, I have seen that the following code compiles in some environments like devc++ but fails on some env's like gcc on linux. Can someone tell if "int *au=malloc(sizeof(int)*10);" is a constant...
4
by: Brett Romero | last post by:
Say I have this setup: public static void SomeMethod() { #if DEBUG .... #endif }
32
by: Zytan | last post by:
Are they possible? I am passing in a large array of Bytes, thus I don't want to use ByVal. Zytan
21
by: Sebastian Faust | last post by:
Hi, Unfortunately, I don't find lots of information on this warning. It occurs if I compile with -pedantic but I am not sure how I can resolve this problem. Do you have an idea? The following...
1
by: hackerbob | last post by:
I'm trying to create a constant time event timer. Basically, a routine can set a callback to be called n ms from the current time, and the main event loop will wait until the delta between the...
8
by: Stefano Sabatini | last post by:
Hi all, I'm encountering this while trying to implement a factory singleton method to generate objects. The singleton has a static map which binds a static creation function defined in each...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.