Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the fundamental difference between source and header files in C?

Tags:

c

I don't quite understand how things should be separated in C's source and header files. I often see many projects with two sets of files with the same name (an extension denotes a source and another donates a header file).

So far, from this lack of understanding, when I've written libraries, I've chucked all the class and class method code into one file, with indecision as to choosing the file extension.

What should be in headers and what should be in the source files? How do I implement this separation?

like image 800
Delan Azabani Avatar asked Aug 14 '10 10:08

Delan Azabani


People also ask

What is the difference between a header file and a source file in C?

So what's the difference between Header files and Source files? Basically, header files are #included and not compiled, whereas source files are compiled and not #included. You can try to side-step these conventions and make a file with a source extension behave like a header or vice-versa, but you shouldn't.

What are source files in C?

When a programmer types a sequence of C programming language statements into Windows Notepad, for example, and saves the sequence as a text file, the text file is said to contain the source code. Source code and object code are sometimes referred to as the "before" and "after" versions of a compiled computer program.

What is header and source files in C++?

The first step that the compiler will do on a source file is run the preprocessor on it. Only source files are passed to the compiler (to preprocess and compile it). Header files aren't passed to the compiler. Instead, they are included from source files.

Is a header file a source code file?

Usually, header files contain declarations, source files contain code. So, if in source file A.c you need a function implemented in source file B.c , you just include B.h to have its declaration.


2 Answers

There is no technical difference. The compiler will happily let you include a .c file, or compile a .h file directly, if you want to.

There is, however, a huge cultural difference:

  • Declarations (prototypes) go in .h files. The .h file is the interface to whatever is implemented in the corresponding .c file.

  • Definitions go in .c files. They implement the interface specified in the .h file.

The difference is that a .h file can (and usually will) be #included into multiple compilation units (.c files). If you define a function in a .h file, it will end up in multiple .o files, and the linker will complain about a multiply defined symbol. That's why definitions should not go in .h files. (Inline functions are the exception.)

If a function is defined in a .c file, and you want to use it from other .c files, a declaration of that function needs to be available in each of those other .c files. That's why you put the declaration in a .h, and #include that in each of them. You could also repeat the declaration in each .c file, but that leads to lots of code duplication and an unmantainable mess.

If a function is defined in a .c file, but you don't want to use it from other .c files, there's no need to declare it in the header. It's essentially an implementation detail of that .c file. In that case, make the function static as well, so it doesn't conflict with identically-named functions in other files.

like image 116
Thomas Avatar answered Oct 12 '22 00:10

Thomas


What should be in headers and what should be in the source files?

Typically headers contain one or more of the following:

  • Function declaration (except statics)
  • Variable declaration (typically global)
  • User defined type declaration (read struct, union etc.)
  • Macro definition

Source files on the other hand have:

  • Function/variable definition
  • Static function declaration and definition (you don't want to expose these to your clients)
  • Variable definition
  • Some prefer to define inline functions (C99) in a header

How do I implement this separation?

The One Definition Rule is your friend.

Remember, if you are writing a library, this is what your client gets to see. So, be helpful and provide all the information you can for them to use your library. The source files are typically compiled and supplied in binary form.

And by the way, C does not have the concept of classes.

like image 21
dirkgently Avatar answered Oct 12 '22 01:10

dirkgently