473,659 Members | 3,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Header files question

mdh
Well, K& R has finally gotten to header files!!!

May I ask this. (Have checked out the FAQ)

I am using "Xcode" to compile the program, so it may well be that this
is doing something strange.

What is unique to a header file?

For example, if I define an integer thus:

i=96;

can I use i in, for example main.c without first declaring it (using
extern i), and if so, what makes the header file diffferent from say,
"other_file .c" with the same definition?

Thanks in advance.

Oct 8 '06 #1
11 1874
mdh
I meant "int i=96;
mdh wrote:
Well, K& R has finally gotten to header files!!!

May I ask this. (Have checked out the FAQ)

I am using "Xcode" to compile the program, so it may well be that this
is doing something strange.

What is unique to a header file?

For example, if I define an integer thus:

int i=96;

can I use i in, for example main.c without first declaring it (using
extern i), and if so, what makes the header file diffferent from say,
"other_file .c" with the same definition?

Thanks in advance.
Oct 8 '06 #2
mdh wrote:
I meant "int i=96;
mdh wrote:
>Well, K& R has finally gotten to header files!!!

May I ask this. (Have checked out the FAQ)

I am using "Xcode" to compile the program, so it may well be that this
is doing something strange.

What is unique to a header file?
Little. It is very much like if you had placed everything in the code
in your .c file instead of saying #include "myheader.h "

>For example, if I define an integer thus:

int i=96;

can I use i in, for example main.c without first declaring it (using
extern i), and if so, what makes the header file diffferent from say,
"other_file. c" with the same definition?
Nothing. If you include a header file with a definition in more than
one .c file, you get mutiple definition - which you don't want.
Define it in exactly one of your .c files, and if you need to
make it available to other .c files, you can e.g. place a
declaration (use the extern keyword) in a header file which
you include where needed.
Oct 8 '06 #3
mdh


mdh wrote:
Well, K& R has finally gotten to header files!!!

May I ask this. (Have checked out the FAQ)
What is unique to a header file?
Nils O. Selåsdal wrote:
Little. It is very much like if you had placed everything in the code
in your .c file instead of saying #include "myheader.h "
int i=96; can I use i in, for example main.c without first declaring it (using
extern i), and if so, what makes the header file diffferent from say,
"other_file .c" with the same definition?

Nothing. ............... place a declaration (use the extern keyword) in a header file which
you include where needed.

So Nils, what you are saying? is
Declarations in the header file?
Definitions in other_c_files?

So, is the designation ".h" simply "convenienc e" if you have a massive
program, it makes it easier debugging etc or if multiple people are
working on it? From what you say, you could put all declarations in
say, "my_specialfile .c", use # include where necessary, but it would
not be as clear?

Thanks

Oct 8 '06 #4
mdh wrote:
Nils O. Selåsdal wrote:
.... snip ...
>>
Nothing. ............... place a declaration (use the extern
keyword) in a header file which you include where needed.

So Nils, what you are saying? is
Declarations in the header file?
Definitions in other_c_files?

So, is the designation ".h" simply "convenienc e" if you have a
massive program, it makes it easier debugging etc or if multiple
people are working on it? From what you say, you could put all
declarations in say, "my_specialfile .c", use # include where
necessary, but it would not be as clear?
You have a fundamental misapprehension in your usage of header
files. Their purpose is not to isolate declarations, but to
publish them for use in other files. They expose the items in your
..c file that should be available elsewhere. A second line of
defense is the 'static' declaration, which prevents items from
being exposed accidentally and thus cluttering the global
namespace, with possible collisions with other files.

--
Some informative links:
<news:news.anno unce.newusers
<http://www.geocities.c om/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html >
<http://www.netmeister. org/news/learn2quote.htm l>
<http://cfaj.freeshell. org/google/>
Oct 8 '06 #5
mdh
mdh wrote:
Nils O. Selåsdal wrote:
... snip ...
>
Nothing. ............... place a declaration (use the extern
keyword) in a header file which you include where needed.
So Nils, what you are saying? is
Declarations in the header file?
Definitions in other_c_files?

So, is the designation ".h" simply "convenienc e" if you have a
massive program, it makes it easier debugging etc or if multiple
people are working on it? From what you say, you could put all
declarations in say, "my_specialfile .c", use # include where
necessary, but it would not be as clear?

You have a fundamental misapprehension in your usage of header
files.
I mean no disrespect, but this is one the reasons I ask...to get rid
of pesky misapprehension s.

>Their purpose is not to isolate declarations, but to publish them for
use in other files. They expose the items in your .c file that should
be available elsewhere.

Ok...I see your point of view and like it. thanks.

>A second line of
defense is the 'static' declaration, which prevents items from
being exposed accidentally and thus cluttering the global
namespace, with possible collisions with other files.
You've lost me on that one...could you possibly expand a little more.

thanks

Oct 8 '06 #6
mdh

CBFalconer wrote:
You have a fundamental misapprehension in your usage of header
files. Their purpose is not to isolate declarations, but to
publish them for use in other files.
I guess this does lead me back to the original essence of the question.
Then, why use the disignation .h as opposed to just using another .c
file to publish them for use in other files. Or, to ask another way,
does the compiler treat a .h file differently from a .c file?
thanks in advance.

Oct 8 '06 #7

"mdh" <md**@comcast.n etwrote in message
news:11******** **************@ m7g2000cwm.goog legroups.com...
>
CBFalconer wrote:
>You have a fundamental misapprehension in your usage of header
files. Their purpose is not to isolate declarations, but to
publish them for use in other files.

I guess this does lead me back to the original essence of the question.
Then, why use the disignation .h as opposed to just using another .c
file to publish them for use in other files. Or, to ask another way,
does the compiler treat a .h file differently from a .c file?
thanks in advance.
Chuck has already described the 'why' of header files. To
be sure you understand, I'll inform (or simply remind) you
that all that happens with #include is exactly that, 'include';
i.e. a 'pasting' of the contents of one file into another, at
the point of the #include directive.

The actual name of the file #included is completely arbitrary.
You could use e.g. ".x", ".y", ".z", or no 'extension' at all.
The ".h" is used by convention, one reason for which is that
that's what the language library's standard headers use(*).

So the answer to your question is no. The compiler doesn't
even see the code until after the #include directives have
already pasted the files together. This 'pasting' is done
by what is known as a 'preprocessor'. Then the compiler sees
the resulting code (known as a 'translation unit').

(*) FYI, the language does not require that these 'standard headers'
be implemented as files, they could be built into the implememtation.
However, they almost always are files (easier for vendors to maintain
and update their implementations ).

HTH,
-Mike
Oct 8 '06 #8
mdh wrote:
CBFalconer wrote:
>You have a fundamental misapprehension in your usage of header
files. Their purpose is not to isolate declarations, but to
publish them for use in other files.

I guess this does lead me back to the original essence of the
question. Then, why use the disignation .h as opposed to just
using another .c file to publish them for use in other files. Or,
to ask another way, does the compiler treat a .h file differently
from a .c file?
For inclusion lines of the form

#include "file.ext"

there is no reliance on the extension. All the compiler does is to
inject the text of the included file at that point in the source
file. The use of .h is just a convenient convention. On file
systems without file extensions the convention can't even be
followed.

Normally one constructs a .h file to match each .c file, selecting
what to publish. This is pointless in programs that consist of
only one .c file.

For lines of the form:

#include <file.h>

things are much different, although in practice only the search
path changes. The compiler is allowed to do anything it wants,
because it knows what the published defines, prototypes, etc. are,
because they are specified in the standard. It may never
physically include anything in this case. The search path will
probably not include the directory in which the .c file resides.

On the use of static, most actual systems generate a relocatable
linkable object file from the compilation. That includes
definitions of entry points, especially to functions. The static
word prevents the publishing of the entry point to a function so
marked. This means that another file can freely use that name
without conflict. These details are system specific, and thus
fairly well off-topic here. Just remember that a static function
is private to the compilation unit.

--
Some informative links:
<news:news.anno unce.newusers
<http://www.geocities.c om/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html >
<http://www.netmeister. org/news/learn2quote.htm l>
<http://cfaj.freeshell. org/google/>
Oct 8 '06 #9
mdh

CBFalconer wrote:
mdh wrote:
CBFalconer wrote:
...............

to Both Chuck and Mike...thank you...
It does clear this up.
Somewhat off-topic, This list is most supportive and appreciated.

Oct 8 '06 #10

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

Similar topics

3
5124
by: dharmesh Gupta | last post by:
Hi All, i am new to header files , and have a basic question , i have made my program in three files namely, idr_bpl.cpp( contains class definitions and function definitions) bpl.cpp( contains main() function) bpl.h (contains variable definition of a variable that is shared in the above two files )
21
2598
by: Hattuari | last post by:
I'm learning C++ after having spent several years in the computer industry doing both system administration and engineering. I've written code in Perl, Bash, Pascal, Ada, C, Mathematica (hundreds of lines of OO code, not 1+1), JavaScript, Lisp, and Java, as well as C++. Each of these languages has it's strengths and weaknesses. My experience with C++ has shown me that it is by far the most difficult for me to learn. I have the sense...
31
2781
by: Steven T. Hatton | last post by:
If a header is not necessarily a source file, and the sequences delimited by < and > in header names aren't necessarily valid source file names, what exactly is a header? -- p->m == (*p).m == p.m http://www.kdevelop.org http://www.suse.com http://www.mozilla.org
6
1747
by: Johannes Bauer | last post by:
Hi group, I've got a question concerning inclusion of .hpp files. Currently I'm including all needed header files in the .cpp file. This means all dependencies of the package and all dependencies of these dependencies and so on. This is quite ugly. A start of an "Example.cpp" file could look like this
18
2243
by: Exits Funnel | last post by:
Hello, I'm a little confused about where I should include header files and was wondering whether there was some convention. Imagine I've written a class foo and put the definition in foo.h and the implementation of its member functions in foo.cpp (which obviously #inludes foo.h) and further assume that it depends on a class bar which is defined in bar.h. It seems that there are the following two scenarios:
3
3087
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.
11
5573
by: ambika | last post by:
Iam just trying to know "c". And I have a small doubt about these header files. The header files just contain the declaration part...Where is the definition for these declarations written??And how does that get linked to our program when we run it??I would appreciate any helpful info..And I would like to thank you for that in advance -ambika
18
2736
by: John Smith | last post by:
Hi all What does the group think of the practise of including one header file from inside another? I have some legacy code where this has been done, and it creates a dependency on a module (collection of files) which are not required, except for one header file's contents. I'd say 'No, header files should be included in the C source, not in another
7
2244
by: The Cool Giraffe | last post by:
Please note that i do intend to use a header file. However, i'm not sure if it's really needed or just a convention. Suppose we have the following two files. // Something.h class Something { private: int number; public: Something ();
0
8335
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
8851
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
8747
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
8528
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
8627
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...
1
6179
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
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
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
1737
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.