473,569 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

#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_CONSTAN TTT" 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_XX X" 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 1798
Timo <t_**********@y ahoo.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_CONSTAN TTT" 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_XX X" 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.hel sinki.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_CONSTAN TTT" 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_CONSTAN TTT" 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_XX X" 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
3180
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 initialization. In C# it is impossible (as far as I know). My question is: Why? In C++ checking for changes to const objects is being done during...
2
7211
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 port it to windows and when i try to compile the same code with the visual studio command line tool "cl" i get this error: const.c(92) : error...
14
2562
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 struct is larger than another struct, etc. I think of something like #define CHECK(expr) static int dummy CHECK(sizeof(struct foo) % 4 == 0);
6
2824
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 expression and can be used in global namespace/file scope. Which part of the standard says or describes this . #include<stdio.h>...
4
2200
by: Brett Romero | last post by:
Say I have this setup: public static void SomeMethod() { #if DEBUG .... #endif }
32
1461
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
8352
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 code produces the warning: const static unsigned long long data = { 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL}
1
2717
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 current time and the earliest event timer has elapsed. When the list is sorted, checking for expiration is O(n) time where n is the number of timers...
8
20400
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 class to the type of the object to be created. Here it is the code, which is a modification of the wikipedia C++ factory example code: ...
0
7693
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...
0
8118
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...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5501
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
3651
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
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
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
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
933
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.