Re: About header files
- From: Richard Heathfield <invalid@xxxxxxxxxxxxxxx>
- Date: Thu, 29 Jun 2006 18:08:32 +0000
Christoph Scholtes said:
Hi,
I have some questions about header files:
Say I have a file functions.c which contains a couple of functions. I
have declared some structs in this file too. The structs are defined in
main.c. Now I create a header file which represents the interface of
functions.c to my main program file main.c.
I put in the header file: all function prototypes with keyword extern
and the declarations of the structs, which are defined in the main
program.
Do I include the header file functions.h in my file functions.c to get
the struct declarations or do I explicitely declare the structs in
functions.c and not include the header file?
Unless you are doing opaque types:
struct type definition goes in header
struct definitions go in the functions that need them
If you are doing opaque types:
struct type definition goes in module's .c file
struct type declaration goes in module's header
pointers to struct types are defined in the functions that need them
About standard libraries: functions.c uses stdio.h, stdlib.h and
string.h.
Those are headers, not libraries.
Thus, I include the header files. main.c also uses stdlib.c
and stdio.h. Do I include them again or do I use inclusion guards to
check if they are already included (e.g. in functions.c)?
Include them as often as you like. They are already protected by inclusion
guards.
If a variable is declared static, is it still legal to pass it on as a
function argument to a function outside of the file?
Yes.
The keyword extern in front of a function prototype in my header file
says that the function is defined somewhere else, right?
I certainly hope so, since you wouldn't want to define the function in the
header file!!
Do I HAVE to
declare the function prototype as extern in the header file?
No. For functions, it's the default, and it can be omitted. Indeed, it
almost always /is/ omitted.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
.
- Follow-Ups:
- Re: About header files
- From: Christoph Scholtes
- Re: About header files
- References:
- About header files
- From: Christoph Scholtes
- About header files
- Prev by Date: Re: which is faster?
- Next by Date: Re: Take and return pointer to function
- Previous by thread: About header files
- Next by thread: Re: About header files
- Index(es):
Relevant Pages
|