473,513 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Three #include guards, which will never break?

Sometimes programmers will define macros at command line like:

$ gcc -DF1_H ...
$ gcc -DF1_H=0 ...
$ gcc -DF1_H=1 ...

One of following three lines labeled as #1, #2, #3 may provide #include
guard and avoid errors may be introduced by redundant #include. Is
there any flaw with each of them? Which one will never break?

#if F1_H /*#1*/
/*#if !defined(F1_H)*/ /*#2*/
/*#ifndef F1_H */ /*#3*/
#define F1_H

/*more stuff here ...*/

#endif
--
lovecreatesbeauty

Jun 7 '06 #1
14 1748
On 2006-06-07, lovecreatesbeauty <lo***************@gmail.com> wrote:
Sometimes programmers will define macros at command line like:

$ gcc -DF1_H ...
$ gcc -DF1_H=0 ...
$ gcc -DF1_H=1 ...

One of following three lines labeled as #1, #2, #3 may provide #include
guard and avoid errors may be introduced by redundant #include. Is
there any flaw with each of them? Which one will never break?

#if F1_H /*#1*/
/*#if !defined(F1_H)*/ /*#2*/
/*#ifndef F1_H */ /*#3*/
#define F1_H

/*more stuff here ...*/

#endif


Most all code I've seen uses #ifndef. That will be the most easily
understood. I've never seen #2 in my life.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.
Jun 7 '06 #2
In article <sl**********************@localhost.localdomain> ,
Andrew Poelstra <ap*******@localhost.localdomain> wrote:
On 2006-06-07, lovecreatesbeauty <lo***************@gmail.com> wrote:
Sometimes programmers will define macros at command line like: $ gcc -DF1_H ...
$ gcc -DF1_H=0 ...
$ gcc -DF1_H=1 ... One of following three lines labeled as #1, #2, #3 may provide #include
guard and avoid errors may be introduced by redundant #include. Is
there any flaw with each of them? Which one will never break? #if F1_H /*#1*/
/*#if !defined(F1_H)*/ /*#2*/
/*#ifndef F1_H */ /*#3*/
#define F1_H
#endif
Most all code I've seen uses #ifndef. That will be the most easily
understood. I've never seen #2 in my life.


#1 is flawed because it tests values rather than definedness.

#2 and #3 are flawed because the # is not the first non-whitespace
on the line, so they are not going to be treated as preprocessor commands.

#ifndef and #if !defined() are exactly the same, but the latter is more
easily extendable without having to go back and change the directive.
#if !defined() definitely does get used.
--
Programming is what happens while you're busy making other plans.
Jun 7 '06 #3
Walter Roberson wrote:
In article <sl**********************@localhost.localdomain> ,
Andrew Poelstra <ap*******@localhost.localdomain> wrote:
On 2006-06-07, lovecreatesbeauty <lo***************@gmail.com> wrote:
Sometimes programmers will define macros at command line like: $ gcc -DF1_H ...
$ gcc -DF1_H=0 ...
$ gcc -DF1_H=1 ... One of following three lines labeled as #1, #2, #3 may provide #include
guard and avoid errors may be introduced by redundant #include. Is
there any flaw with each of them? Which one will never break? #if F1_H /*#1*/
/*#if !defined(F1_H)*/ /*#2*/
/*#ifndef F1_H */ /*#3*/
#define F1_H
#endif

Most all code I've seen uses #ifndef. That will be the most easily
understood. I've never seen #2 in my life.


#1 is flawed because it tests values rather than definedness.

#2 and #3 are flawed because the # is not the first non-whitespace
on the line, so they are not going to be treated as preprocessor commands.


#2 and #3 are commented-out /alternatives/ to #1.

--
Chris "seeker" Dollin
"I'm still here and I'm holding the answers" - Karnataka, /Love and Affection/

Jun 7 '06 #4
In article <e6**********@canopus.cc.umanitoba.ca>,
Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote:
The commenting around the directives are *part* of lines #2 and #3.
There is no indication that we are to remove the commenting in our
consideration.


The reader is expected to apply common sense.

-- Richard

Jun 7 '06 #5
Al Balmer <al******@att.net> wrote:
Andrew Poelstra <ap*******@localhost.localdomain> wrote:
lovecreatesbeauty <lo***************@gmail.com> wrote:

#if F1_H /*#1*/
/*#if !defined(F1_H)*/ /*#2*/
/*#ifndef F1_H */ /*#3*/
#define F1_H

/*more stuff here ...*/

#endif


Most all code I've seen uses #ifndef. That will be the most easily
understood. I've never seen #2 in my life.


No? I prefer it, actually. It's more versatile and can be extended to
more complex tests if needed.


Such as the following. The rationale in this case was to stop
lint-like tools from complaining about undefined preprocessor symbols
when using the more common "#ifdef ..." form.

/* Use -DSTANDALONE_TEST=1 in the makefile to enable */
/* statistics collection and profiling timers. */

#if !defined(STANDALONE_TEST)
# define STANDALONE_TEST 0
#endif

#if defined(STANDALONE_TEST) && (STANDALONE_TEST==1)
/* insert test/debug code here */
...
#endif
Jun 7 '06 #6
On Wed, 07 Jun 2006 13:46:43 GMT, Andrew Poelstra
<ap*******@localhost.localdomain> wrote:
On 2006-06-07, lovecreatesbeauty <lo***************@gmail.com> wrote:
Sometimes programmers will define macros at command line like:

$ gcc -DF1_H ...
$ gcc -DF1_H=0 ...
$ gcc -DF1_H=1 ...

One of following three lines labeled as #1, #2, #3 may provide #include
guard and avoid errors may be introduced by redundant #include. Is
there any flaw with each of them? Which one will never break?

#if F1_H /*#1*/
/*#if !defined(F1_H)*/ /*#2*/
/*#ifndef F1_H */ /*#3*/
#define F1_H

/*more stuff here ...*/

#endif


Most all code I've seen uses #ifndef. That will be the most easily
understood. I've never seen #2 in my life.


No? I prefer it, actually. It's more versatile and can be extended to
more complex tests if needed.

--
Al Balmer
Sun City, AZ
Jun 7 '06 #7
In article <e6**********@malatesta.hpl.hp.com>,
Chris Dollin <ch**********@hp.com> wrote:
Walter Roberson wrote:
On 2006-06-07, lovecreatesbeauty <lo***************@gmail.com> wrote:
#2 and #3 are flawed because the # is not the first non-whitespace
on the line, so they are not going to be treated as preprocessor commands.

#2 and #3 are commented-out /alternatives/ to #1.


Re-examine the original posting:
One of following three lines labeled as #1, #2, #3 may provide #include
guard and avoid errors may be introduced by redundant #include. Is
there any flaw with each of them? Which one will never break? #if F1_H /*#1*/
/*#if !defined(F1_H)*/ /*#2*/
/*#ifndef F1_H */ /*#3*/
#define F1_H
#endif


The commenting around the directives are *part* of lines #2 and #3.
There is no indication that we are to remove the commenting in our
consideration. We are told, "One of the following three lines", not
"One of the following three lines, after having the leading comment
delimiters removed on the second and third line but not touching the
comment delimiters around the # labelling".
--
Prototypes are supertypes of their clones. -- maplesoft
Jun 7 '06 #8
Richard Tobin schrieb:
Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote:
The commenting around the directives are *part* of lines #2 and #3.
There is no indication that we are to remove the commenting in our
consideration.


The reader is expected to apply common sense.


In comp.lang.c?

SCNR
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 7 '06 #9
Roberto Waltman <us****@rwaltman.net> writes:
Such as the following. The rationale in this case was to stop
lint-like tools from complaining about undefined preprocessor symbols
when using the more common "#ifdef ..." form.

#if !defined(STANDALONE_TEST)
# define STANDALONE_TEST 0
#endif
Given the above, you don't need the defined(...) test below,
because STANDALONE_TEST will always be defined.
#if defined(STANDALONE_TEST) && (STANDALONE_TEST==1)
/* insert test/debug code here */
...
#endif

--
"C has its problems, but a language designed from scratch would have some too,
and we know C's problems."
--Bjarne Stroustrup
Jun 7 '06 #10
In article <e6***********@pc-news.cogsci.ed.ac.uk>,
Richard Tobin <ri*****@cogsci.ed.ac.uk> wrote:
In article <e6**********@canopus.cc.umanitoba.ca>,
Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote:
The commenting around the directives are *part* of lines #2 and #3.
There is no indication that we are to remove the commenting in our
consideration.

The reader is expected to apply common sense.


Not in answering what is obviously a homework or test question:
such questions must be read literally because the literal reading
is what the marker is expecting.

It would have been easy enough for the test-poser to use three
different sections if the commenting out had not been intended -as-
commenting out.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Jun 7 '06 #11
Ben Pfaff <bl*@cs.stanford.edu> wrote:
Roberto Waltman <us****@rwaltman.net> writes:
#if !defined(STANDALONE_TEST)
# define STANDALONE_TEST 0
#endif


Given the above, you don't need the defined(...) test below,
because STANDALONE_TEST will always be defined.
#if defined(STANDALONE_TEST) && (STANDALONE_TEST==1)
/* insert test/debug code here */
...
#endif


You are right, of course. I pasted a few lines from two different
files and saw that after posting. I seems I am having another low
coffee pressure incident...
Jun 7 '06 #12
Walter Roberson wrote:
In article <e6**********@malatesta.hpl.hp.com>,
Chris Dollin <ch**********@hp.com> wrote:
Walter Roberson wrote:
On 2006-06-07, lovecreatesbeauty <lo***************@gmail.com> wrote: #2 and #3 are flawed because the # is not the first non-whitespace
on the line, so they are not going to be treated as preprocessor commands.
#2 and #3 are commented-out /alternatives/ to #1.


Re-examine the original posting:
One of following three lines labeled as #1, #2, #3 may provide #include
> guard and avoid errors may be introduced by redundant #include. Is
> there any flaw with each of them? Which one will never break? #if F1_H /*#1*/
> /*#if !defined(F1_H)*/ /*#2*/
> /*#ifndef F1_H */ /*#3*/
> #define F1_H
> #endif


The commenting around the directives are *part* of lines #2 and #3.


Yes, I see that. That's what makes those lines "commented out". Were
the lines uncommented, the example text would be illegal, or at
least incomplete.
There is no indication that we are to remove the commenting in our
consideration.
There's no indication that we should assume the question is
sensible, either, but that hasn't stopped you treating it as
such.
We are told, "One of the following three lines", not
"One of the following three lines, after having the leading comment
delimiters removed on the second and third line but not touching the
comment delimiters around the # labelling".


That's why we prefer informal to formal languages when communicating
with people about the non-critical parts of a problem.

Ain't English great?

--
Chris "never uses 'ain\'t'" Dollin
A rock is not a fact. A rock is a rock.

Jun 8 '06 #13

Chris Dollin wrote:
Walter Roberson wrote:
We are told, "One of the following three lines", not
"One of the following three lines, after having the leading comment
delimiters removed on the second and third line but not touching the
comment delimiters around the # labelling".


That's why we prefer informal to formal languages when communicating
with people about the non-critical parts of a problem.


Thanks. I'm not a native English speaker and always feel uneasy to
express myself in English. It's not my homework, I have no homework for
a long time after I graduated.

Years ago, I read a C coding guideline and it mentioned the usage of
#if expr or #if !defined(expr) for #include guard instead of #ifndef
expr. I can not find that document again, I'm not clear why it insisted
on using #if or #if !defined() but not #ifndef.

Jun 8 '06 #14
lovecreatesbeauty wrote:
Years ago, I read a C coding guideline and it mentioned the usage of
#if expr or #if !defined(expr) for #include guard instead of #ifndef
expr. I can not find that document again,
I'm not clear why it insisted
on using #if or #if !defined() but not #ifndef.


I prefer #ifndef because the next line is #define
and I like the way that those two lines line up.

/* BEGIN file.h */

#ifndef H_FILE_H
#define H_FILE_H
/*
**
*/
#endif

/* END file.h */

--
pete
Jun 8 '06 #15

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

Similar topics

28
3833
by: Ramesh | last post by:
Hi, I am currently maintaining a legacy code with a very very large code base. I am facing problems with C/C++ files having a lot of un-necessary #includes. On an average every C/C++ file has...
44
3314
by: Neil Cerutti | last post by:
In Rob Pike's style guide he urges the following: Simple rule: include files should never include include files. If instead they state (in comments or implicitly) what files they need...
17
2928
by: meital | last post by:
There are three kinds of stones: red,green and blue. Each cell of the array contains one stone. The array needs to be sorted so that all the red stones will come first, then the blue ones and...
5
2495
by: David Mathog | last post by:
One thing that can make porting C code from one platform to another miserable is #include. In particular, the need to either place the path to an included file within the #include statement or to...
6
1932
by: Ben Taylor | last post by:
I've asked about includes before in this group in order to stop files being #included either twice or not at all, and somebody said to use extern keyword for global variables that need to be used...
5
1585
by: mailtogops | last post by:
Hi All, This is very basic C/C++ question but I need internals of C/C++ how the langauge/compiler do this.. Say I have two header files 1. header1.h 2. header2.h
14
22330
by: Pedro Graca | last post by:
Imagine I have a structure with a size_t member: /* foo.h */ struct foo { char const *bar; size_t barlen; }; void make_foo(struct foo *p);
3
6895
by: linq936 | last post by:
Hi, I have a C++ header file like this, #include "MyClass1.h" class MyClass2{ private: MyClass1* c1; };
18
2123
by: gutmant | last post by:
Say you have a file, a.h with an include guard. If you include it twice and look at the preprocessed output, you see there's no sign for the second inclusion. However, if you include it twice -...
0
7175
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
7391
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
7553
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...
0
7542
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...
0
5697
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,...
1
5100
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...
0
3235
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1609
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 ...
0
466
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...

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.