custom header files

J

Jared

Hi,

Can someone explain the basic structure of a header file? I just
completed a introductory C programming class. The course stressed
structured programming and creating reusable code. We learned to use
functions and to create our own. The problem is we never learned to
place them in header files. To me, and obviously, I'm not that
experienced with C, it would be better to place user created functions
in separate header files instead of in the main .c source code file,
right? At any rate, I would greatly appreciate it if someone could
explain this to me.

thanks in advance,
Jared
 
J

Joona I Palaste

Jared said:
Can someone explain the basic structure of a header file? I just
completed a introductory C programming class. The course stressed
structured programming and creating reusable code. We learned to use
functions and to create our own. The problem is we never learned to
place them in header files. To me, and obviously, I'm not that
experienced with C, it would be better to place user created functions
in separate header files instead of in the main .c source code file,
right? At any rate, I would greatly appreciate it if someone could
explain this to me.

Header files should contain only prototypes of the user created
functions, not the function bodies themselves. That way you can
include the header file from multiple source files and not get any
linker errors.
 
O

osmium

Jared said:
Can someone explain the basic structure of a header file? I just
completed a introductory C programming class. The course stressed
structured programming and creating reusable code. We learned to use
functions and to create our own. The problem is we never learned to
place them in header files. To me, and obviously, I'm not that
experienced with C, it would be better to place user created functions
in separate header files instead of in the main .c source code file,
right? At any rate, I would greatly appreciate it if someone could
explain this to me.

You didn't use quite the magic words. I think what you really want to know
about is separate compilation, which involves headers files and other things
(such as, perhaps, make files or "projects") as well. But that is a topic
related to the compiler, not the language, so is off-topic here. I suggest
doing a google advanced groups search to get your feet wet and then post a
question to a compiler oriented newsgroup. You *can*, indeed, put actual
code in a header file (it probably works) but it is bad practice in C. The
header itself simply contains function prototypes and stuff like that, the
actual *code* is in another .c file. In an introductory course you don't
even learn about the _linker_ which eventually ties all this assorted stuff
together.
 
P

Peter Shaggy Haywood

Groovy hepcat Jared was jivin' on 12 Jan 2004 23:40:54 -0800 in
comp.lang.c.
custom header files's a cool scene! Dig it!
Can someone explain the basic structure of a header file? I just

Headers don't have any particular structure. They're just C source
files that are inserted into a translation unit at compile time. (A
translation unit is a C source file and all the headers it includes,
and, recursively, all the headers they include, compiled as one
logical unit.) Headers also define macros and data types needed by the
translation unit.
But headers are intended to declare functions and variables that may
be defined in other translation units. One or more compiled
translation units may be linked together to create a program.
Typically one compiled translation unit or a collection of them
comprises a library. A header usually declares the functions and
global data in the library and defines data types used by the library
and any macros that may be used to interface with the library. When
your program uses a library, you include its header in your code. This
tells your translation unit(s) how to interface with the library.
(Note that this applies not only to libraries, but also to programs
that have several translation units that are not a part of a library.)
completed a introductory C programming class. The course stressed
structured programming and creating reusable code. We learned to use
functions and to create our own. The problem is we never learned to
place them in header files. To me, and obviously, I'm not that

You don't put functions (definitions) in headers. You put function
*declarations* there, as well as type definitions, macro definitions
and variable declarations. You should never put executable code in a
header. You put the function and variable definitions that these
declarations refer to in a separate translation unit (if you desire -
and if you're concerned about reusable code, you ought to desire)
which also includes the header.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

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
474,135
Messages
2,570,783
Members
47,341
Latest member
hanifree

Latest Threads

Top