473,671 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

#define

I have defined some variables using #define preprocessor instruction.
And then later I checked whether I had defined a variable. Sometimes
even though a variable have been defined, #if !defined(var) construct
is returning true. I am using gcc3.0.1 on SunOS8. I would appreciate
if any body can tell me whether this is a bug or I am doing something
wrong.

tia,
Srinivas
Jul 19 '05 #1
20 25420
srinivas reddy <sr************ *@yahoo.com> wrote in message
news:ff******** *************** ***@posting.goo gle.com...
I have defined some variables using #define preprocessor instruction.
And then later I checked whether I had defined a variable. Sometimes
even though a variable have been defined, #if !defined(var) construct
is returning true. I am using gcc3.0.1 on SunOS8. I would appreciate
if any body can tell me whether this is a bug or I am doing something
wrong.


I would have said that this surely is a bug, but I wouldn't put anything
past the C++ preprocessor.

Incomprehensibl y, #if var1 == var2 simply converts var1 and var2 to "0"
(yes, "0", even though the preprocessor is a _text_ replacer) if it hasn't
come across definitions of them (something like Basic assuming that any
undefined variable it comes across must be an int; and I thought C++ got rid
of implicit this and implicit that because they are thought unsafe). 0 == 0
is true, of course.

I can only assume that those with influence who wish to see the end of the
preprocessor altogether are trying to accelerate its death by ensuring that
it works as badly as possible.

DW

Jul 19 '05 #2
David White wrote:

I can only assume that those with influence who wish to see the end of the
preprocessor altogether are trying to accelerate its death by ensuring that
it works as badly as possible.


Then it must be that the folks who originally came up with the idea of
the preprocessor thirty years ago tried to accelerate its death, because
replacing undefined symbols with 0 in arithmetic expressions has been
the rule since the beginning.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 19 '05 #3

"srinivas reddy" <sr************ *@yahoo.com> wrote in message
news:ff******** *************** ***@posting.goo gle.com...
I have defined some variables using #define preprocessor instruction.
And then later I checked whether I had defined a variable. Sometimes
even though a variable have been defined, #if !defined(var) construct
is returning true. I am using gcc3.0.1 on SunOS8. I would appreciate
if any body can tell me whether this is a bug or I am doing something
wrong.

tia,
Srinivas


Perhaps you have defined the variable, but not in the compilation unit in
which your #if statement is written? In other words, if you #define the
variable in unit1.h, and make your check in unit2.cpp, then you need to add
#include "unit1.h" in unit2.cpp before checking if the variable exists.

(I usually put my #define's in a precompiled header if they're going to be
widely used in my projects. But that's with CodeWarrior...I don't know how
to use gcc so it may be different.)

Just a thought...

-Howard

Jul 19 '05 #4
David White wrote:
srinivas reddy <sr************ *@yahoo.com> wrote in message
news:ff******** *************** ***@posting.goo gle.com...
I have defined some variables using #define preprocessor instruction.
And then later I checked whether I had defined a variable. Sometimes
even though a variable have been defined, #if !defined(var) construct
is returning true. I am using gcc3.0.1 on SunOS8. I would appreciate
if any body can tell me whether this is a bug or I am doing something
wrong.


I would have said that this surely is a bug, but I wouldn't put
anything past the C++ preprocessor.

Incomprehensibl y, #if var1 == var2 simply converts var1 and var2 to
"0" (yes, "0", even though the preprocessor is a _text_ replacer) if
it hasn't come across definitions of them (something like Basic
assuming that any undefined variable it comes across must be an int;
and I thought C++ got rid of implicit this and implicit that because
they are thought unsafe). 0 == 0 is true, of course.

I can only assume that those with influence who wish to see the end
of the preprocessor altogether are trying to accelerate its death by
ensuring that it works as badly as possible.


It isn't the preprocessor that is bad--even the conversion to 0 that you mention
here. It is *misuse* of the preprocessor that is bad. The preprocessor is
actually a critical component of the C and C++ compilation process. It makes it
possible to write code that works on multiple platforms, as well as write code
that works on various current compilers (as opposed to the idealistic perfect
C++ implementation) .

Regards,
Paul Mensonides
Jul 19 '05 #5
Pete Becker <pe********@acm .org> wrote in message
news:3F******** *******@acm.org ...
David White wrote:

I can only assume that those with influence who wish to see the end of the preprocessor altogether are trying to accelerate its death by ensuring that it works as badly as possible.


Then it must be that the folks who originally came up with the idea of
the preprocessor thirty years ago tried to accelerate its death, because
replacing undefined symbols with 0 in arithmetic expressions has been
the rule since the beginning.


I accept that, but why hasn't it been fixed along with everything else?
Implicit int, matching of function argument types, insistence that function
definitions be present, etc. have been some of the many improvements to C++
since C. I don't think anyone disputes that these are all good things. The
more prgrammer errors you can detect at compile time the better. Why leave
something there that's so obviously bad?

DW

Jul 19 '05 #6
David White wrote:

Pete Becker <pe********@acm .org> wrote in message
news:3F******** *******@acm.org ...
David White wrote:

I can only assume that those with influence who wish to see the end of the preprocessor altogether are trying to accelerate its death by ensuring that it works as badly as possible.

Then it must be that the folks who originally came up with the idea of
the preprocessor thirty years ago tried to accelerate its death, because
replacing undefined symbols with 0 in arithmetic expressions has been
the rule since the beginning.


I accept that, but why hasn't it been fixed along with everything else?


Because it's not broken.
Implicit int, matching of function argument types, insistence that function
definitions be present, etc. have been some of the many improvements to C++
since C. I don't think anyone disputes that these are all good things. The
more prgrammer errors you can detect at compile time the better. Why leave
something there that's so obviously bad?


The fact that you don't understand it doesn't make it bad.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 19 '05 #7
Pete Becker <pe********@acm .org> wrote in message
news:3F******** *******@acm.org ...
David White wrote:


I accept that, but why hasn't it been fixed along with everything else?


Because it's not broken.
Implicit int, matching of function argument types, insistence that function definitions be present, etc. have been some of the many improvements to C++ since C. I don't think anyone disputes that these are all good things. The more prgrammer errors you can detect at compile time the better. Why leave something there that's so obviously bad?

The fact that you don't understand it doesn't make it bad.


What have I said that indicates that I don't understand it? Did I describe
it wrongly?

I'm interested to know: do you think that assuming that an undefined
preprocessor symbol is "0" is a good thing, or something that wouldn't be
improved by a compiler error saying that the symbol is undefined? If so, why
not extend the principle to assuming that any symbol in a C++ expression is
an 'int'?

myVariable = 7;
// myVariable not defined anywhere: so it must be an 'int'.

Okay?

myVariable = myFunction(3, "abc", 2.65);
// myFunction not defined anywhere: so it must be int myFunction(int, char
*, double);

Okay?

DW

Jul 19 '05 #8
David White wrote:

Pete Becker <pe********@acm .org> wrote in message
news:3F******** *******@acm.org ...
David White wrote:


I accept that, but why hasn't it been fixed along with everything else?
Because it's not broken.
Implicit int, matching of function argument types, insistence that function definitions be present, etc. have been some of the many improvements to C++ since C. I don't think anyone disputes that these are all good things. The more prgrammer errors you can detect at compile time the better. Why leave something there that's so obviously bad?

The fact that you don't understand it doesn't make it bad.


What have I said that indicates that I don't understand it? Did I describe
it wrongly?


You said earlier that the preprocessor is "a _text_ replacer."

I'm interested to know: do you think that assuming that an undefined
preprocessor symbol is "0" is a good thing, or something that wouldn't be
improved by a compiler error saying that the symbol is undefined?
No. It would make some things much more verbose, and would only help
beginners.
If so, why
not extend the principle to assuming that any symbol in a C++ expression is
an 'int'?


Non sequitur.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 19 '05 #9
Pete Becker <pe********@acm .org> wrote in message
news:3F******** *******@acm.org ...
David White wrote:

What have I said that indicates that I don't understand it? Did I describe it wrongly?
You said earlier that the preprocessor is "a _text_ replacer."


Yes, and that statement was _clearly_ made in the context of #define and
#if.

#define X Y

Doesn't this replace the symbol 'X' found anywhere in the source code with
the text 'Y'?

Also: "Because they rearrange the program text before the compiler proper
sees it, macros are..." - The C++ Programming Language (3rd ed.), page 160.

Given that macros _do_ replace text, why should an undefined symbol become
'0' rather than ''?
I'm interested to know: do you think that assuming that an undefined
preprocessor symbol is "0" is a good thing, or something that wouldn't be improved by a compiler error saying that the symbol is undefined?


No. It would make some things much more verbose,


Such as?

And is the increased verbosity worse than no message from the compiler when
a symbol is used without having been defined?

Speaking of verbosity, the way to ensure that preprocessor symbols are not
silently converted to 0 is:

#if !defined(REACTO R_TYPE) || !defined(REACTO R_NEW_MODEL)
#error REACTOR_TYPE or REACTOR_NEW_MOD EL not defined
#endif

Apart from the fact that if one remembers to do this then one would have
ensured that the symbols were defined, is it not verbose to place this in
every source file in which these symbols are used?
and would only help
beginners.


I see. So, only beginners would ever forget to ensure that both of these are
#defined somewhere?

#if REACTOR_TYPE == REACTOR_NEW_MOD EL
If so, why
not extend the principle to assuming that any symbol in a C++ expression is an 'int'?


Non sequitur.


void f(int reactorType)
{
// No definition of REACTOR_NEW_MOD EL given
if(reactorType == REACTOR_NEW_MOD EL)
{
// ...
}
}

Why should this be an error, but not the preprocessor version?

DW

Jul 19 '05 #10

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

Similar topics

18
8917
by: Bryan Parkoff | last post by:
"#define" can only be inside the global scope before main() function. "#if" can be tested after "#define" is executed. The problem is that "#define" can't be inside main() function. I do not wish to create multiple functions which they look almost identical in C++ source code. The C++ compiler should be able to compile one Test() function into two almost identical functions before they are translated into the machine language object. ...
2
5588
by: Andreas | last post by:
Hi! I'm using an IplImage library from camellia.sourceforge.net, and the testbench calls a file only containing code like the one below. In cam_morphomaths_code.c the real computation is made, but I don't understand where cam_morphomaths_code.c is included and how to set what parameters to use in the testbench. How does this code execure? I don't understand a bit...
3
10571
by: theotyflos | last post by:
Hi all, I have the following: /*--- SNIP ---*/ typedef struct Argument_s { char *address; int type;
9
3090
by: pozz | last post by:
Hi all, I have the below #defines #define NUMBER1 30 #define NUMBER2 50 #define SUM (NUMBER1+NUMBER2) #define STRING1 "Byte: \x30" #define STRING2 "Byte: \x50" If I change NUMBER1 and NUMBER2 I must change STRING1 and STRING2 consequently.
34
3193
by: BQ | last post by:
Hello Is there a way to declare 'FUNCT' via a define so that if its parameter x, a constant, is greater than 35, it returns 56, if not, 20. I would like that at compile time, not at run time. So: #define FUNCT(x) x>35?56:20
17
2770
by: niraj.tiwari | last post by:
What is meaning of the following define:- #define x(argl...) x1(##argl)
71
33187
by: David T. Ashley | last post by:
Where is the best place to define TRUE and FALSE? Are they in any of the standard include files, ever? Do any standards apply? What I've traditionally done is something like: #ifndef (TRUE) #define TRUE (1)
6
27838
by: anirbid.banerjee | last post by:
Hi, I need to write a macro which would have a lot many conditional #ifdef ... #endif blocks in it. #define _xx_macro (x) { ... \ ... \ /* some code (); */ #ifdef _SOME_STMT \ ... \ ... \
23
3906
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
2
2294
by: badc0de | last post by:
Hello.. a header file of one of my project has macro definitions like this (for example) #define PART_A_SORT_1_LABEL_STRING1 100 #define PART_A_SORT_1_LABEL_STRING2 200 #define PART_A_SORT_1_LABEL_STRING3 300 #define PART_A_SORT_1_LABEL_STRING4 400 #define PART_A_SORT_1_LABEL_STRING5 500
0
8388
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8907
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8663
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7423
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6218
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5687
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4396
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1799
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.