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

About header files

P: n/a
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
Jun 29 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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)
Jun 29 '06 #2

P: n/a
Richard Heathfield wrote:
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.


And that is the case for all standard headers?
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.


Interesting. Thank you.

Chris
Jun 29 '06 #3

P: n/a
Christoph Scholtes wrote:
Richard Heathfield wrote:
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.


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

Jun 29 '06 #4

P: n/a


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

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.

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.

--
Er*********@sun.com

Jun 29 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.