About header files

C

Christoph Scholtes

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?

About standard libraries: functions.c uses stdio.h, stdlib.h and
string.h. 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)?

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? In my case I have
declared a struct in my file functions.c, "exported" it via functions.h
to main.c and defined a variable with it as static. main.c calls a
function from functions.c using this statically defined variable. It
works, but why? Isnt the scope of the variable limited to main.c?

The keyword extern in front of a function prototype in my header file
says that the function is defined somewhere else, right? Do I HAVE to
declare the function prototype as extern in the header file? If I remove
the extern my program still compiles flawlessly.

Thanks,
Chris
 
R

Richard Heathfield

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.
 
C

Christoph Scholtes

Richard said:
Include them as often as you like. They are already protected by inclusion
guards.

And that is the case for all standard headers?
No. For functions, it's the default, and it can be omitted. Indeed, it
almost always /is/ omitted.

Interesting. Thank you.

Chris
 
R

Robert Gamble

Christoph said:
And that is the case for all standard headers?

Yes. From the Standard, 7.1.2p4:

"Standard headers may be included in any order; each may be included
more than once in
a given scope, with no effect different from being included only once,
except that the
effect of including <assert.h> depends on the definition of NDEBUG (see
7.2)."

Robert Gamble
 
E

Eric Sosman

Christoph Scholtes wrote On 06/29/06 15:26,:
Richard Heathfield wrote:




And that is the case for all standard headers?

With one exception, the Standard headers can be
included as many times as you like (and in any order
you like) with exactly the same effect as if they had
been included just once. The exception is <assert.h>,
whose effect depends on whether the NDEBUG macro is
or is not defined at each point of inclusion:

#define NDEBUG
#include <assert.h>
... assert()s disabled here ...
#undef NDEBUG
... assert()s still disabled ...
#include <assert.h>
... assert()s enabled here ...

Technically speaking, the Standard headers can use
"compiler magic" instead of inclusion guards to get
multiple inclusions to behave like just one. The headers
are part of the implementation, just like the compiler
itself, and the Standard describes what the implementation
must do but does not prescribe how to do it.
 

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
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top