Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Header files question

Reply
Thread Tools

Header files question

 
 
mdh
Guest
Posts: n/a
 
      10-08-2006
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.

 
Reply With Quote
 
 
 
 
mdh
Guest
Posts: n/a
 
      10-08-2006
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.


 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=
Guest
Posts: n/a
 
      10-08-2006
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.
 
Reply With Quote
 
mdh
Guest
Posts: n/a
 
      10-08-2006



> > 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 "convenience" 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

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-08-2006
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 "convenience" 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.announce.newusers
<http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>


 
Reply With Quote
 
mdh
Guest
Posts: n/a
 
      10-08-2006
> 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 "convenience" 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 misapprehensions.


>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

 
Reply With Quote
 
mdh
Guest
Posts: n/a
 
      10-08-2006

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.

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-08-2006
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.announce.newusers
<http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-08-2006

"mdh" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.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


 
Reply With Quote
 
mdh
Guest
Posts: n/a
 
      10-08-2006

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Header files with "header.h" or <header.h> ?? mlt C++ 2 01-31-2009 02:54 PM
UNIX header files to Windows header files anand.ba@gmail.com C Programming 3 05-01-2006 03:57 PM
Header files included in header files John Smith C Programming 18 07-24-2004 04:55 AM
What is better /standard for creating files. a cpp file with header or cpp and seperate file for header DrUg13 C++ 1 02-10-2004 09:20 AM
how to avoid using another header file inside a header file? Newsgroup - Ann C++ 4 11-02-2003 01:20 PM



Advertisments