473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

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

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.netwrote in message
news:11**********************@m7g2000cwm.googlegro 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
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.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/>
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
>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?
No, except sometimes if you give the .h file name as the name of a
compilation unit to compile.

But *PROGRAMMERS* treat them differently. The convention that foo.h
contains the external interface: declarations of functions, types,
and structures needed to use the code in foo.c is a useful one.

Often you can compile a whole program with many compilation units
with a command line like:

cc -o foo *.c

which won't work if you attempt to compile the header files as compilation
units on their own and they are named header.c .

Because compiler front-ends often use a file suffix to determine
what type a file is, the suffix you give to the compiler as the
file to compile may matter. For example, a .c suffix may indicate
C, a .cpp or .c++ or .C suffix may indicate C++, a .f suffix may
indicate FORTRAN, a .o file or .obj file or .a file is a compiled
object file which you don't compile but just pass to the linker.
A .h file might be compiled by the Hurd compiler, or be passed
directly to the linker, be ignored, or cause an error. All of this
is very system-dependent.

In this case, if bar.c contains:

#include "bar.h"

and bar.h contains a definition of main(),

the command
cc -o bar bar.h
may fail but the command
cc -o bar bar.c
may succeed. But there isn't a difference between including a .h
file and including a .c file with the same contents.

Oct 8 '06 #11
mdh

Gordon Burditt wrote:
>

No, except sometimes if you give the .h file name as the name of a
compilation unit to compile.

But *PROGRAMMERS* treat them differently. .......
thanks

Oct 8 '06 #12

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

Similar topics

3
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(...
21
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...
31
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...
6
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...
18
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...
3
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...
11
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...
18
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...
7
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 {...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.