Possible Duplicate:
What are the point of header files in C?
I am working on a unix project. After searching a lot , many suggest use of header files as they are used to write code that need to be shared between several source files.
It is best to centralise the definitions in one file – Header File.
Now I got two doubts:
1) What is the significance of using header file over simply .c files.
As from this answer I get that header files are not compiled to object files. Then What happen(actually how gcc treats them)? What is the benefit? What the answer I shared want to point?
2) What are the rules or convention to write a header files. I only know this:
Header files usually ONLY contain definitions of data types,
function prototypes and C preprocessor commands.
Can we write only this i.e is this is a rule or convention?
Can anyone tell me any source to learn how to write header files.
I am able to find only this –Writing Larger Programs-which does not contain much. Or any other tip or style for writing better and optimized header files.
1) header files allow you to pass information between .c files, e.g., definitions, or the existence of functions in other .c files, which allows you to keep code that logically belongs together in one place; the #include directive treats the named file as if it were part of the file in which it is named, which allows you to spare a lot of redundant typing
2) the convention is to pass #define’s, enum’s, and function declarations into .h files, with actual functional code (besides the occasional static inline function) in .c files
The rule is: whatever the compiler will eat without dying is more or less allowed, as long as it results in a functional program. The conventions exist more or less to help you keep your projects in an understandable, maintainable state.