Question about (Optional)? header files.

M

mdh

K&R briefly mention splitting programs into seperate files.(page 82)
Using my compiler (X-code) I noticed that creating a header file
gives, as expected one .h file, but creating a new ".c" file creates
an additional file, same name, but with an ".h" extension, and calls
this an optional header file.
Compiling the program using both the .c and .h ( with appropriate
#includes) executes successfully. In K&R, .h files were used to
centralize declarations, etc, and each .c file was used to define
functions etc.
Could anyone explain what is happening here, and perhaps suggest good
usage? ( I looked at the FAQ, and unless I missed it, I did not see
this addressed.)
As usual, thanks in advance.
 
S

santosh

mdh said:
K&R briefly mention splitting programs into seperate files.(page 82)
Using my compiler (X-code) I noticed that creating a header file
gives, as expected one .h file, but creating a new ".c" file creates
an additional file, same name, but with an ".h" extension, and calls
this an optional header file.
Compiling the program using both the .c and .h ( with appropriate
#includes) executes successfully. In K&R, .h files were used to
centralize declarations, etc, and each .c file was used to define
functions etc.
Could anyone explain what is happening here, and perhaps suggest good
usage? ( I looked at the FAQ, and unless I missed it, I did not see
this addressed.)
As usual, thanks in advance.

Generally header files are used for exporting and importing declarations
and definitions. Say that you have implemented a regular expression
parser library in files r0.c, r1.c and r2.c. Obviously these modules
will need to share some common declarations. A data structure might
need to be global, appropriate typedefs may need to be present and r0.c
may call functions from r1.c, r1.c from r2.c and in other combinations.
Here is where a header file comes into use. You place all the
appropriate declarations into a header, say r.h, and #include this into
all the necessary .c files.

Also your library will export an interface to the world which will need
to be put into one or more headers and supplied along with your source
files.

So, in summary, headers either export some functionality to the outside
world, or import functionality into modules from other modules or serve
as a place to put declarations and definitions that will be used by
multiple modules, like macros and feature test conditionals etc.

The Standard library headers are themselves a good illustration of the
concepts involved. Unfortunately the heavy use of compiler magic causes
most headers from mainstream C libraries to be rather difficult for a
beginner to understand.
 
K

Keith Thompson

mdh said:
K&R briefly mention splitting programs into seperate files.(page 82)
Using my compiler (X-code) I noticed that creating a header file
gives, as expected one .h file, but creating a new ".c" file creates
an additional file, same name, but with an ".h" extension, and calls
this an optional header file.
Compiling the program using both the .c and .h ( with appropriate
#includes) executes successfully. In K&R, .h files were used to
centralize declarations, etc, and each .c file was used to define
functions etc.
Could anyone explain what is happening here, and perhaps suggest good
usage? ( I looked at the FAQ, and unless I missed it, I did not see
this addressed.)

This wouldn't be mentioned in the FAQ, because it's not a C issue.

I'm not familiar with X-code, but I suspect it's some kind of IDE, not
just a compiler. Apparently it chooses to generate a .h file for you
in some circumstances. Such a file should be no different from one
you could have written manually.

The compiler itself doesn't care how any soure file was generated.
 
C

CBFalconer

mdh said:
.... snip ...

Compiling the program using both the .c and .h ( with appropriate
#includes) executes successfully. In K&R, .h files were used to
centralize declarations, etc, and each .c file was used to define
functions etc.

You are misusing header files. Their purpose is to expose elements
of a .c file that are needed in other .c files. They are, for
example, the method of promulgating the prototype of a function to
other .c files. Things that are not needed externally should not
be mentioned in the .h file, and in fact should often be marked
static in the .c file.
 
C

Charlton Wilbur

KT> I'm not familiar with X-code, but I suspect it's some kind of
KT> IDE, not just a compiler.

Xcode (spelling counts!) is Apple's IDE, which uses a customized
version of gcc as its compiler. (I *think* the customizations all
relate to Objective-C, but I wouldn't swear to it.) There's a lot of
DWIM in it.

The best place to ask about Xcode proper is the xcode-users list at Apple.

Charlton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top