473,804 Members | 4,269 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
20 25449
"Paul Mensonides" <le******@comca st.net> wrote in message
news:ZoHOa.1206 5$H17.3639@sccr nsc02...
David White wrote:
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.
Well, I think the conversion to 0 _is_ bad. Given that you can use #ifdef or
#if defined() for things such as:
#ifdef _cplusplus

how can the implicit conversion to 0 of an undefined symbol be a good thing?
Why is it better than issuing an error?
It is *misuse* of the preprocessor that is bad. The preprocessor is
actually a critical component of the C and C++ compilation process.
I agree. That's why I'd like it to work _safely_.
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) .


Yes, but I want to do it safely. I do not want the outcome of an #if to be
one of these two possibilities:
1. The result of the expression of previously defined symbols.
2. A programmer's mistake in forgetting to include the defined symbols.

This is inherently unsafe. The possibility of no. 2 is the reason that C++
insists on all function definitions being present and that there is a
suitable match for every argument. Does not one other person here think that
this is a problem?

DW

Jul 19 '05 #11

David White wrote:
[...]
This is inherently unsafe. The possibility of no. 2 is the reason that C++
insists on all function definitions being present and that there is a
suitable match for every argument. Does not one other person here think that
this is a problem?


http://groups.google.com/groups?thre...01%40hsv3.UUCP
(Subject: Using a define that hasn't been #defined)

regards,
alexander.

--
"Status quo, you know, that is Latin for ``the mess we're in.''"

-- Ronald Reagan
Jul 19 '05 #12

"David White" <no@email.provi ded> wrote in message news:Kb******** **********@nasa l.pacific.net.a u...

Doesn't this replace the symbol 'X' found anywhere in the source code with
the text 'Y'?
No, it doesn't. Pete is right, you seem not to understand the preprocessor.
Given that macros _do_ replace text, why should an undefined symbol become
'0' rather than ''?


They do not replace text, they replace tokens.
Jul 19 '05 #13
Ron Natalie wrote:
"David White" <no@email.provi ded> wrote in message
news:Kb******** **********@nasa l.pacific.net.a u...

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


No, it doesn't. Pete is right, you seem not to understand the
preprocessor.
Given that macros _do_ replace text, why should an undefined symbol
become '0' rather than ''?


They do not replace text, they replace tokens.


Even more specifically, they replace macro invocations:

#define X() Y

X // X

Regards,
Paul Mensonides
Jul 19 '05 #14
David White wrote:
It isn't the preprocessor that is bad--even the conversion to 0 that
you mention here.
Well, I think the conversion to 0 _is_ bad. Given that you can use
#ifdef or #if defined() for things such as:
#ifdef _cplusplus

how can the implicit conversion to 0 of an undefined symbol be a good
thing? Why is it better than issuing an error?


Because it is a "reasonable default." Reasonable defaults make code less
verbose. This happens with templates also:

template<class T> void f(int reactorType)
{
// No definition of REACTOR_NEW_MOD EL given
if(reactorType == T::REACTOR_NEW_ MODEL)
{
// ...
}
}

The compiler will pass this with no problem even though it still parses the
expression, etc.. The reasonable default here is "non-type". The point being
that the language has to deal with unknown names and make assumptions about what
they mean in various places.

That is just the way it is. You know what the behavior is, so writing "safe"
code is up to you to use the language in safe ways. C and C++ certainly don't
protect you from unsafe usage many areas, why should they do that here?

If you changed the behavior to an error, how would you do this in a non-verbose
way:

# if !__cplusplus && __STDC_VERSION_ _ >= 199901L

You'd have to do something really annoying because you cannot use any
conditional test that uses the name outside the defined operator. You can't
even do this:

#if defined(__STDC_ VERSION__) && __STDC_VERSION_ _ >= 199901L

...because that constitutes an error under your model if __STDC_VERSION_ _ is not
defined. You'd have to separate the test for definition from the conditional
expression:

# if !defined __cplusplus && defined __STDC_VERSION_ _
# if __STDC_VERSION_ _ >= 199901L
# // 1
# else
# // 2
# endif
# else
# // 2
# endif

....and that is a code doubler for point 2.

If you changed the behavior to expanding to nil instead of 0, you'd have silent
changes in other ways. You'd also end of seeing a lot of "hacks" like this:

# if !(__cplusplus+0 ) && (__STDC_VERSION __+0) >= 199901L

In order to simulate the common scenario that we already have built into the
preprocessor.
It is *misuse* of the preprocessor that is bad. The preprocessor is
actually a critical component of the C and C++ compilation process.


I agree. That's why I'd like it to work _safely_.


It does work safely if used correctly. I said it before already: The #if and
#elif directives are not designed to implicitly perform the kind of verification
that you want--because that kind of verification (if done by default) is
downright annoying.

Further, the root problem here is 1) forgetting to include a file, or 2) design
error. Assuming that it is just a case of forgetting to include the file that
defines the symbols, there a many ways in which a program can silently change
meaning in C++ by not including a file (e.g. silently choosing different
function overloads or different template specializations ).
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) .


Yes, but I want to do it safely. I do not want the outcome of an #if
to be one of these two possibilities:
1. The result of the expression of previously defined symbols.


It is totally ill-conceived. You can do what you want reasonably, but you
cannot do what it already does reasonably. You already have the option to do
what you want:

#if defined(REACTOR _TYPE) \
&& defined(REACTOR _NEW_MODEL) \
&& REACTOR_TYPE == REACTOR_NEW_MOD EL

You can't go back the other way.
2. A programmer's mistake in forgetting to include the defined
symbols.

This is inherently unsafe.
No it isn't _inherently_ unsafe. It can be unsafe in certain contexts, and you
have to be aware of that when you write code. However, the alternative is much
worse. You can simulate what you want with a small amount of code; you cannot
simulate what it already does with a small amount of code.
The possibility of no. 2 is the reason
that C++ insists on all function definitions being present and that
there is a suitable match for every argument. Does not one other
person here think that this is a problem?


C++ does not insist on all function declarations that you've defined in a group
of files be present at each overload resolution--which can cause silent
differences in overload resolution, etc..

Regards,
Paul Mensonides
Jul 19 '05 #15
Ron Natalie <ro*@sensor.com > wrote in message
news:3f******** *************** @news.newshosti ng.com...

"David White" <no@email.provi ded> wrote in message news:Kb******** **********@nasa l.pacific.net.a u...

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


No, it doesn't. Pete is right, you seem not to understand the

preprocessor.

I do. I just didn't use precise enough language in this pedantic place. I am
aware that the 'X' in 'MAX' would not be replaced by 'Y'.
Given that macros _do_ replace text, why should an undefined symbol become '0' rather than ''?


They do not replace text, they replace tokens.


Okay. Why you didn't correct Stroustrup's loose language as well? :)

Getting off the track.

DW

Jul 19 '05 #16
Alexander Terekhov <te******@web.d e> wrote in message
news:3F******** *******@web.de. ..

David White wrote:
[...]
This is inherently unsafe. The possibility of no. 2 is the reason that C++ insists on all function definitions being present and that there is a
suitable match for every argument. Does not one other person here think that this is a problem?


http://groups.google.com/groups?thre...01%40hsv3.UUCP
(Subject: Using a define that hasn't been #defined)


Thanks, Alexander. I am not alone after all.

DW

Jul 19 '05 #17
On Thu, 10 Jul 2003 13:35:24 +1000, David White <no@email.provi ded> wrote:
Sam Holden <sh*****@flexal .cs.usyd.edu.au > wrote in message
news:slrnbgpln5 .fft.sh*****@fl exal.cs.usyd.ed u.au...
On Thu, 10 Jul 2003 12:31:36 +1000,
David White <no@email.provi ded> wrote:
>

Using #if in anything but a header file is madness in my opinion. I
guess using it to give some platform specific optimisation might be OK,
but I'd try and find a way to move the #if itself into a header.


You can't always use C++ itself. How, for example, do you make a declaration
depend on whether you are compiling a NORMAL or LITE version of the
software? Suppose you also have EXTRA_LITE and PREMIUM versions. And suppose
the following constant has a different value for each one:

#if MODEL == EXTRA_LITE
const int NrWheels = 2;
#elif MODEL == LITE
const int NrWheels = 4;
// etcetera


By moving those values into the header file, since they are obviously
in some way dependant on the MODEL and hence having them in one place
rather than scattered throughout many source files is nice (even
if they are in fact only used once - it unifies the location of all
such data).

#define MODEL_NRWHEELS 2
or
#define MODEL_NRWHEELS 4

which get set by the appropriate model specific header.

and then

const int NrWheels = MODEL_NRWHEELS;

Has the disadvantage that when the code is changed, sometimes
the model header files will have to be modified as well. But
has the advantage that when adding another model you will
hopefully just have to add the appropriate header file set
a the appropriate define and all the code now works - as opposed
to finding all those #ifs and adding #elif MODEL == NEW_MODEL.
Of course in practice things never work out and you have
to modify a bit of the code.

But I'd personally be using the C++ type system rather than
#defines to tell things apart. I've been bitten by inconsistant
settings in #defines in different compile runs or files in C
too many times.

--
Sam Holden

Jul 19 '05 #18
David White wrote:
It's not going to change. No matter how much you complain about it.


Yes, I realized that very quickly. I can't even get anyone here to
agree that anything needs to be fixed.


It isn't that I don't agree that the kind of danger you refer to doesn't exist.
Instead, its that I don't consider it a big enough problem to prohibit the
utility of the implicit conversion to 0. IMO, the benefits outweigh the costs.

Regards,
Paul Mensonides
Jul 19 '05 #19

"David White" <no@email.provi ded> wrote in message news:%G******** *****@nasal.pac ific.net.au...
Ron Natalie <ro*@sensor.com > wrote in message I do. I just didn't use precise enough language in this pedantic place. I am
aware that the 'X' in 'MAX' would not be replaced by 'Y'.
There's much more to it than that.
They do not replace text, they replace tokens.


Okay. Why you didn't correct Stroustrup's loose language as well? :)


Because Stroustrup wasn't wrong. You were. The only reference to text
on the page you reference says "the preprocessor rearranges the text ..."
He doesn't say anything specific about macro replacment
Getting off the track.


You're the one driving us there.

Jul 19 '05 #20

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

Similar topics

18
8930
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
5594
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
10588
by: theotyflos | last post by:
Hi all, I have the following: /*--- SNIP ---*/ typedef struct Argument_s { char *address; int type;
9
3097
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
3216
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
2780
by: niraj.tiwari | last post by:
What is meaning of the following define:- #define x(argl...) x1(##argl)
71
33242
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
27905
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
3923
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
2301
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
9579
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
10330
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10076
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
9144
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
7616
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
6851
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
5520
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.