473,883 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Header include order

It seems like, in every C source file I've ever seen, there has been a
very definite include order, as follows:

- include system headers
- include application headers
- include the header associated with this source file

For example, in a file hello.c:

#include <stdio.h>
#include "utils.h"
#include "hello.h"

(Incidentally I think that a source file which doesn't include the header
file which exports its symbols is _very_ bad, as this is a good way to
check for inconsistencies for free.)

I would argue that the standard order of header including is wrong,
and that the correct order is the reverse. Consider this scenario:

hello.c:
#include <stdlib.h>
#include "hello.h"

hello.h:
struct blah {
size_t size;
};

hello2.c
#include "hello.h"

Inexplicably (from the perspective of the person doing the including)
the file hello.h will cause compiler errors in hello2.c but not in hello.c.
If hello.c were written first, and then the include file used elsewhere,
the error would appear to be "new", and not be caught by those who wrote
hello.c, implementing the functionality exported by hello.h.

If this include order is used, this problem is averted:

- include the header associated with this source file
- include application headers
- include system headers

This is good for two reasons:
1. All headers must now include any system headers they need, and will
fail immediately if they don't.
2. Every header will be included in at least ONE source file before
anything else (the source file associated with that header), allowing
any intra-application dependencies to be caught.

Does anyone have a reasonable justification for the standard include
order that I haven't thought of? Thanks.

--
Derrick Coetzee
Nov 13 '05
60 8335
Alan Balmer wrote:

On Wed, 19 Nov 2003 14:17:57 -0800, "E. Robert Tisdale"
<E.************ **@jpl.nasa.gov > wrote:
Where is the definition for size_t?


In a header file included by stdlib.h


You'd have been right with either stddef.h or stdio.h.

The standard does not specify that stdlib.h includes any other files.


... but the Standard *does* specify that <stdlib.h>
defines `size_t'. 7.20, paragraph 2.

--
Er*********@sun .com
Nov 13 '05 #21
On Thu, 20 Nov 2003 11:01:48 -0500, Eric Sosman <Er*********@su n.com>
wrote:
Alan Balmer wrote:

On Wed, 19 Nov 2003 14:17:57 -0800, "E. Robert Tisdale"
<E.************ **@jpl.nasa.gov > wrote:
>> Where is the definition for size_t?
>
>In a header file included by stdlib.h


You'd have been right with either stddef.h or stdio.h.

The standard does not specify that stdlib.h includes any other files.


... but the Standard *does* specify that <stdlib.h>
defines `size_t'. 7.20, paragraph 2.


You're right, of course. I thought I remembered only three places
(above plus wchar.h). Should have double-checked.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 13 '05 #22
In <1a************ *************** *****@4ax.com> Alan Balmer <al******@att.n et> writes:
On 20 Nov 2003 12:02:08 GMT, Da*****@cern.ch (Dan Pop) wrote:
What do you call

struct foo foo_thing;

?


An object definition. Are you sure you've read the parenthetical note
in my previous post?


That's why I asked the question. You said that your naming there
applied to types, not objects, but did not specify your convention for
describing object declarations/definitions.


I'm using the standard terminology, no point in inventing my own: if it
reserves space, it's an object definition, otherwise it's a mere
declaration.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #23
"Arthur J. O'Dwyer" wrote:
On Thu, 20 Nov 2003, CBFalconer wrote:
"E. Robert Tisdale" wrote:

You are confused.
A file included by the C preprocessor directive
is *not* necessarily a header file.
A file with *.h extension is *not* necessarily a header file.
Neither the C preprocessor or the C programming language
specify file name extensions for header files.
We should take especial notice now. That omniscient expert, ERT,
has made his pronouncement. Since he specifies his organization
as "Jet Propulsion Laboratory", and his return address as
@jpl.nasa.gov, he carries very imposing credentials. After all,
his abilities and careful standards adherence and understanding
are obviously protecting the entire US space program. The utter
drivel posted in c.l.c under his name is obviously a concerted
plot by the evil (left/right/center) to undermine his shining
reputation.

.... snip ...
So, let's leave the name-calling for the defenses of 'void
main' and 'malloc' casting, announcements of 'obvious trolls',
and indictments of 'indigenous plonkers', huh? :-) IMHO
Tisdale is right for once.


Where did I call him names? I referred to him as an "omniscient
expert". I attributed any misinformation posted (allegedly) by
him to a plot by evil fringe groups? However I should probably
have stripped all of his post apart from the "you are confused"
portion.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!

Nov 13 '05 #24
Alan Balmer wrote:

On Thu, 20 Nov 2003 11:01:48 -0500, Eric Sosman <Er*********@su n.com>
wrote:
Alan Balmer wrote:

On Wed, 19 Nov 2003 14:17:57 -0800, "E. Robert Tisdale"
<E.************ **@jpl.nasa.gov > wrote:

>> Where is the definition for size_t?
>
>In a header file included by stdlib.h

You'd have been right with either stddef.h or stdio.h.

The standard does not specify that stdlib.h includes any other files.


... but the Standard *does* specify that <stdlib.h>
defines `size_t'. 7.20, paragraph 2.


You're right, of course. I thought I remembered only three places
(above plus wchar.h). Should have double-checked.


Also <string.h>. The Standard's practice seems to be
that any header defines all the types used in its function
declarations. There are a few exceptions (e.g., <stdio.h>
doesn't define va_list even though vfprintf() et al. use
it), but they are rare.

I always get tripped up by `offsetof'. For some insane
reason I expect <stdlib.h> to provide it, but of course it's
found only in <stddef.h>. Evidence of a warped childhood,
I guess.

--
Er*********@sun .com
Nov 13 '05 #25
Alan Balmer <al******@att.n et> wrote:
On Wed, 19 Nov 2003 14:17:57 -0800, "E. Robert Tisdale"
<E.************ **@jpl.nasa.gov > wrote:
Where is the definition for size_t?


In a header file included by stdlib.h

You'd have been right with either stddef.h or stdio.h. The standard does not specify that stdlib.h includes any other files.


Turns out I was incorrect.

A.3.11 GENERAL UTILITIES <stdlib.h>

EXIT_FAILURE
EXIT_SUCCESS
<...>
size_t

However, the fact that this is accomplished by the
inclusion of another header file, within stdlib.h,
is a speculation. It is not mandated by the standard.

Alex

Nov 13 '05 #26
>"Arthur J. O'Dwyer" wrote:
So, let's leave the name-calling for the defenses of 'void
main' and 'malloc' casting, announcements of 'obvious trolls',
and indictments of 'indigenous plonkers', huh? :-) IMHO
Tisdale is right for once.

In article news:3F******** *******@yahoo.c om
CBFalconer <cb********@wor ldnet.att.net> writes:Where did I call him names? I referred to him as an "omniscient
expert". I attributed any misinformation posted (allegedly) by
him to a plot by evil fringe groups?
I imagine Arthur interpreted this as sarcasm. (I certainly did. :-) )
However I should probably have stripped all of his post apart from
the "you are confused" portion.


Whenever I see that line, I am reminded of the old "rogue" game.
I imagine my "@" character wandering about the dungeon, looking
for the Amulet of Yendor, and encountering a "T":

The Trollsdale hits! -more-

You are confused.

:-) (Of course, in rogue, it was a different monster that caused
the "confusion" , after which one's attempts to move or hit caused
a random direction to be chosen. Trolls merely hit very hard, and
had good built-in armor.)

(At the U of MD, we once managed to get hold of the source to an
older version of rogue, and someone "customized " it to put in the
names of various professors and other characters: "The Zelkowitz
hits! Your PhD is denied!" And so on.)

(Seriously, though, I do think he uses it more as a command than
a statement.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://67.40.109.61/torek/index.html (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 13 '05 #27
Alex wrote:
Alan Balmer <al******@att.n et> wrote:
E. Robert Tisdale wrote:


Where is the definition for size_t?

In a header file included by stdlib.h

You'd have been right with either stddef.h or stdio.h.


The standard does not specify that stdlib.h includes any other files.

Turns out I was incorrect.

A.3.11 GENERAL UTILITIES <stdlib.h>

EXIT_FAILURE
EXIT_SUCCESS
<...>
size_t

However, the fact that this is accomplished by the
inclusion of another header file, within stdlib.h,
is a speculation. It is not mandated by the standard.


That's irrelevant.
All you need to do to prove my assertion false
is to present *one* example of an implementation
where size_t is defined in stdlib.h and *not*
in some included header file.

Nov 13 '05 #28
E. Robert Tisdale <E.************ **@jpl.nasa.gov > wrote:
Alex wrote:
Alan Balmer <al******@att.n et> wrote:
E. Robert Tisdale wrote:


>Where is the definition for size_t?

In a header file included by stdlib.h

You'd have been right with either stddef.h or stdio.h.


The standard does not specify that stdlib.h includes any other files.

Turns out I was incorrect.

A.3.11 GENERAL UTILITIES <stdlib.h>

EXIT_FAILURE
EXIT_SUCCESS
<...>
size_t

However, the fact that this is accomplished by the
inclusion of another header file, within stdlib.h,
is a speculation. It is not mandated by the standard.

That's irrelevant.
All you need to do to prove my assertion false
is to present *one* example of an implementation
where size_t is defined in stdlib.h and *not*
in some included header file.


If you insist. Solaris 2.6:

/usr/include/stdlib.h:

<...>

#ifndef _SIZE_T
#define _SIZE_T
typedef unsigned int size_t;
#endif

<...>

Alex
Nov 13 '05 #29
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote:
Alex wrote:
Alan Balmer <al******@att.n et> wrote:
E. Robert Tisdale wrote:

>Where is the definition for size_t?

In a header file included by stdlib.h

The standard does not specify that stdlib.h includes any other files.


A.3.11 GENERAL UTILITIES <stdlib.h>

size_t

However, the fact that this is accomplished by the
inclusion of another header file, within stdlib.h,
is a speculation. It is not mandated by the standard.


That's irrelevant.
All you need to do to prove my assertion false
is to present *one* example of an implementation
where size_t is defined in stdlib.h and *not*
in some included header file.


Nope. All we need to do to prove your assertion false is to note that
the Standard doesn't demand that <stdlib.h> includes any header files,
but does demand that it define size_t.

Richard
Nov 13 '05 #30

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

Similar topics

8
6434
by: qazmlp | last post by:
I need to include a list of - C++ headers - headers of other modules - headers of my module - implementation specific ie.OS headers In what order, they should be included in my .CPP file? What is the best followed approach?
7
2098
by: header_question | last post by:
Is it best to place all #include files in one header file seperate from all the other files in a project? What is the logic that determines the order of the #include files, does it depend on the order of function calls in main?
11
2767
by: Steven T. Hatton | last post by:
In the past there have been lengthy discussiions regarding the role of header files in C++. People have been very adamat about header files serving as in interface to the implementation. I do understand the objective. This example may represent an interface in need of a bit of refactoring, but it goes to demonstrate the basic idea as I understand it. http://developer.kde.org/documentation/library/cvs-api/kdevelop/html/ast_8h-source.html...
3
3106
by: pooja | last post by:
Suppose i have created a class c1 with f1()in c1.cpp and included this c1.cpp in file1.cpp file , which is also having main() by giving the statement #include "c1.cpp". the same i can do by using header file. i can create a class c1 with f1() in c1.h and include this c1.h in file1.cpp by giving the statement #include "c1.h" tell me that what exactly is the difference between c1.h and c1.cpp? Since they both are doing the same things.
8
3059
by: nrhayyal | last post by:
Hi c++ Gurus, Need your blessing. while testing few aspects with respect to header file inclusions, i observed few things which i would like to share with you. i have a file sqlca.h in which a structure sqlca is declared. i included this file as a soft link in /usr/include. the soft link is as follows: sqlca.h -> /usr/opt/db2_08_01/include64/sqlca.h
3
2755
by: fc2004 | last post by:
Hi, Is there any tools that could report where cyclic header dependency happens? this would be useful when working with a large project where tens or hundreds of headers files may form complex inclusion relationships. Another question I have is how to solve this dependency when "typedef" is used. usually we can use forward declaration like "struc A; " to solve references like "struct A* p". But in many places we have
12
2108
by: Ben | last post by:
I'm kind of new to creating templates. I've made some small class and function templates in the past and I have used quite of bit of the STL, but I am having problems tyring to create templates. I'm trying to make class templates in seperate header files with pointers to each other, and I am getting strange errors. Is it possible to do?
1
1754
by: Marco Spatz | last post by:
Hi, I used PCLint to check my code yesterday and got some warnings about "Repeated include files" and "Redundant declaration for symbol 'CLASSNAME'". I know the reasons for these warnings and I think they are not a problem. But all this made me think about the order in which header files are include. Think about a 4 layered architecture with
11
2816
by: Gary Wessle | last post by:
Hi is it right to have a line like #include <path/to/header.hfor a library on my system, in my header file and use some functions provided by this library in the implementation file (file.cpp) inside a class with out declaring those functions in the class declaration in the header file? thanks
0
10734
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
10836
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
10407
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
9568
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...
0
7114
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
5794
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...
1
4607
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
2
4211
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3230
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.