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