Lot of the times when I watch other people's code I see some are including a .h file and some are including a .c/.cpp file. What is the difference?
-
4Technically nothing, you can include any filename you like, and the file is just opened and copied in place. Typically, `.h` files hold the *declarations* of functions, etc, and `.cpp` files hold *definitions* (which you don't need to see to call the function). It is rather odd to `#include` a `.cpp` file. – BoBTFish Aug 25 '14 at 09:35
-
2The main difference is, that including `.c`/`.cpp` files almost always leads to serious trouble. – πάντα ῥεῖ Aug 25 '14 at 09:37
-
What kind of trouble? – odbhut.shei.chhele Aug 25 '14 at 09:42
-
1@eddard.stark _"Multiple definition"_ errors during linking phase in most cases – πάντα ῥεῖ Aug 25 '14 at 09:43
-
If it is not too much trouble, then can you please explain it to me or give me link, please, thank you :) – odbhut.shei.chhele Aug 25 '14 at 09:44
-
1@eddard.stark See [here please](http://stackoverflow.com/a/25139592/1413395) (and the question accordingly). – πάντα ῥεῖ Aug 25 '14 at 09:47
4 Answers
It depends on what is in the file(s).
The #include
preprocessor directive simply inserts the referenced file at that point in the original file.
So what the actual compiler stage (which runs after the preprocessor) sees is the result of all that inserting.
Header files are generally designed and intended to be used via #include
. Source files are not, but it sometimes makes sense. For instance when you have a C file containing just a definition and an initializer:
const uint8_t image[] = { 128, 128, 0, 0, 0, 0, ... lots more ... };
Then it makes sense to make this available to some piece of code by using #include
. It's a C file since it actually defines (not just declares) a variable. Perhaps it's kept in its own file since the image is converted into C source from some other (image) format used for editing.

- 391,730
- 64
- 469
- 606
-
So what should I put in the .h file and what should I put in the .c file? – odbhut.shei.chhele Aug 25 '14 at 09:43
-
Should I put the function prototypes in the .h file and the function in the .c file? – odbhut.shei.chhele Aug 25 '14 at 09:45
-
2@eddard.stark Yes, that's very common. Headers are used to describe the *public* interface to a module of code implemented by a C file. Do not include any `static` function prototypes, they're not public. – unwind Aug 25 '14 at 09:46
.h files are called header files, they should not contain any code (unless it happens to contain information about a C++ templated object). They typically contain function prototypes, typedefs, #define statements that are used by the source files that include them. .c files are the source files. They typically contain the source code implementation of the functions that were prototyped in the appropriate header file.
Source- http://cboard.cprogramming.com/c-programming/60805-difference-between-h-c-files.html

- 73
- 8
you can look at gcc website (https://gcc.gnu.org/onlinedocs/gcc/Invoking-G_002b_002b.html) that reports a good summary of all the extensions that you can use in C/C++:
C++ source files conventionally use one of the suffixes ‘.C’, ‘.cc’, ‘.cpp’, ‘.CPP’, ‘.c++’, ‘.cp’, or ‘.cxx’; C++ header files often use ‘.hh’, ‘.hpp’, ‘.H’, or (for shared template code) ‘.tcc’; and preprocessed C++ files use the suffix ‘.ii’. GCC recognizes files with these names and compiles them as C++ programs even if you call the compiler the same way as for compiling C programs (usually with the name gcc).

- 606
- 6
- 9
Including header file with declarations is the main, recommended and used almost anywhere, method for making consistent declarations among a project. Including another source file is another (very rare) kind of beast, and it's useful and possible under specific conditions:
- There is a reason to split code to separate source files despite it shall be compiled as a single module. For example, there are different versions of some functions which shan't be visible from another modules. So, they are declared
static
but which version is included is regulated by compile options. Another variant is size and/or maintanenance credentials issues. - The included file isn't compiled by itself as a project module. So, its exported definitions aren't in conflict with the module that file is included to.
Here, I used terms definition and declaration in the manner that the following are declarations:
extern int qq;
void f(int);
#define MYDATATYPE double
and the following are definitions:
int qq; // here, the variable is allocated and exported
void f(int x) { printf("%d\n", x); } // the same for function
(Also, declarations include C++ methods with bodies declared inside their class definition.)
Anyway, the case another .c/.cxx/etc. file is included into source file are very confusing and shall be avoided until a very real need. Sometimes a specific suffix (e.g. .tpl) is used for such files, to avoid reader's confusion.

- 4,171
- 1
- 19
- 31