Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old November 14th, 2005, 01:35 AM
Jared
Guest
 
Posts: n/a
Default custom header files

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
Default 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
Default 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
Default 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"?
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,248 network members.