Connecting Tech Pros Worldwide Help | Site Map

custom header files

  #1  
Old November 14th, 2005, 01:35 AM
Jared
Guest
 
Posts: 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
  #2  
Old November 14th, 2005, 01:35 AM
Joona I Palaste
Guest
 
Posts: n/a

re: custom header files


Jared <jgivens@tpg.com.au> scribbled the following:[color=blue]
> Hi,[/color]
[color=blue]
> 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.[/color]

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 (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Make money fast! Don't feed it!"
- Anon
  #3  
Old November 14th, 2005, 01:36 AM
osmium
Guest
 
Posts: n/a

re: custom header files


Jared writes:
[color=blue]
> 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.[/color]

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.


  #4  
Old November 14th, 2005, 01:47 AM
Peter Shaggy Haywood
Guest
 
Posts: n/a

re: custom header files


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!
[color=blue]
>Can someone explain the basic structure of a header file? I just[/color]

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.)
[color=blue]
>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[/color]

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"?
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
what are the rules for writing header files? ypjofficial@indiatimes.com answers 24 January 30th, 2006 02:45 AM
Creating a custom project in VC++ .NET 2003 50295@web.de answers 2 November 17th, 2005 05:35 PM
PostgreeSQL C header files Christian Kienle answers 3 November 12th, 2005 01:36 AM
Creating a custom project in VC++ .NET 2003 50295@web.de answers 2 October 7th, 2005 07:25 PM