2

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.

Community
  • 1
  • 1
Abhishek Gupta
  • 6,465
  • 10
  • 50
  • 82

2 Answers2

2

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.

tbert
  • 2,089
  • 13
  • 14
  • For answer to Ist ques. We can get all this functionality by writing .c file and then including that. Then why this? For answer to 2nd ques. According to you, I am having function declarations in .h file and definition in .c file then, if I include the .h file (which is having the declaration), the .c file (with definition) will be included itself or I have to write different include statement to include the .c file Simply , I want to learn how to write the same. Is there any source, book in which it is explained with examples? – Abhishek Gupta Apr 12 '12 at 12:05
  • because, as stated, #include includes the text of the file named, and if you name a .c file, it includes that. the .h file convention is for information which is supposed to be shared between various .c files. If you're asking "why have .h files at all", the answer is basically to be able to share code between .c files without having to include them directly. – tbert Apr 12 '12 at 12:12
  • As I shared a specific answer of a question above which explains a little bit more about why to share code with .h. I need only a bit explanation of that. – Abhishek Gupta Apr 12 '12 at 12:20
  • No, the second .c file will not be included automatiacally, you need to compile it into a .o file and then take all the .o files and link them together to create the full executable. Really, look up the difference between compiling and linking, as this appears to be where you're missing a really important bit of knowledge. – tbert Apr 12 '12 at 12:25
0

I think the best is to understand why header files are useful (2) and how they work (1). Then, you can decide yourself what conventions to follow or not.

1) The "include" directive has the effect that the file pointed in the directive is completely included in the currently compiled file. This should be straightforward.

2) Generally it is easier to manage a C project split into multiple C files. This can be because of size reasons (think about projects with thousands lines of code - how can you scroll in such a file) but also for managing reasons (think about multiple people working on the project - easier to split responsibilities).

In a multiple C file project you will want to invoke functions from the other files. Let's give an example:

fileA.c
int function_a() {
  uses somewhere function_b
}

fileB.c
int function_b() {
  uses somewhere function_c 
}

fileC.c
int function_c() {
  uses somewhere function_a
}

In order to invoke any function, you need first to declare it (or define it). To be clear: a declaration is when the return type and parameters are specified, a definition is when the code is also specified. In the example above there is no way you can just "include" one C file into another such that all functions are defined before they are used. So, you have to add a "declaration" of the functions before they are called. Where can you add such a declaration? If you add a declaration of function_c in fileA.c this is very hard to maintain (think you can have this scenario over tens of files, it will be very hard to track all the declarations).

The same thing applies to types.

A solution to this problem is to create for each C file a "header" file in which you declare all the functions (and types) that are needed by other modules of the program. This files can be included in all other C files that need those functions, in any order, and are a centralized place which you know you need to modify if you change a function.

vladmihaisima
  • 2,119
  • 16
  • 20
  • 1
    As the solution is to create for each C (say A.c) file a "header" (say A.h) file. And if we want to use functions of this header file in different c (say B.c) file we have to include this header file(A.h) not he c file (A.c) – Abhishek Gupta Apr 12 '12 at 12:22
  • @knoxxs Yes, that is the intended use that is also the convention. – HonkyTonk Apr 12 '12 at 12:44