By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,301 Members | 1,381 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,301 IT Pros & Developers. It's quick & easy.

custom header files

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Jared <jg*****@tpg.com.au> scribbled the following:
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.


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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Make money fast! Don't feed it!"
- Anon
Nov 14 '05 #2

P: n/a
Jared writes:
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.
Nov 14 '05 #3

P: n/a
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"?
Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.