What is the difference between embedding a .c file and a .h file

Many times, when I look at other people's code, I see that some of them contain a .h file, and some of them contain a .c / .cpp file. What is the difference?

+5
source share
4 answers

It depends on what is in the file.

The #include preprocessor directive simply inserts the reference file at this point in the source file.

What the real stage of the compiler sees (which works after the preprocessor) is the result of all this insertion.

Header files are usually designed and intended to be used through #include . There are no source files, but sometimes it makes sense. For example, if you have a C file containing only 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 using #include . This is a C file because it actually defines (and not just declares) a variable. Perhaps it is stored in its own file, since the image is converted to source C from another (image) format used for editing.

+7
source

.h files are called header files, they should not contain any code (unless it contains information about the C ++ object template). Usually they contain prototypes of functions, typedefs, #define, which are used by the source files that include them. .C files are source files. Usually they contain an implementation of the source code of the functions that were prototyped in the corresponding header file.

Source- http://cboard.cprogramming.com/c-programming/60805-difference-between-hc-files.html

+2
source

you can look at the gcc website ( https://gcc.gnu.org/onlinedocs/gcc/Invoking-G_002b_002b.html ), which provides a good summary of all the extensions you can use in C / C ++

C ++ source files usually use one of the suffixes .C, '.cc,'. Cpp, '.CPP,'. C ++, '.cp or'.cxx; C ++ header files often use ".hh", ".hpp", ".H" or (for common template code) ".tcc; and pre-processed C ++ files use the suffix '.ii. GCC recognizes files with by these names and compiles them as C ++ programs, even if you call the compiler in the same way as for compiling C programs (usually with the name gcc).

+1
source

The inclusion of a header file with declarations is the main, recommended and used almost anywhere, way to create consistent declarations between the project. The inclusion of another source file is another (very rare) kind of beast, and it is useful and possible under certain conditions:

  • There is a reason to split the code into separate source files, even though it must be compiled as a single module. For example, there are different versions of some functions that should not be visible from other modules. Thus, they are declared static , but which version is included is governed by compilation options. Another option is the issue of credentials in terms of size and / or accounting.
  • The included file does not compile on its own as a project module. Thus, its exported definitions do not conflict with the module in which the file is included.

Here I used a definition of terms and a declaration such that the following declarations:

 extern int qq; void f(int); #define MYDATATYPE double 

and the following definitions:

 int qq; // here, the variable is allocated and exported void f(int x) { printf("%d\n", x); } // the same for function 

(In addition, declarations include C ++ methods with bodies declared inside their class definition.)

In any case, the case is different .c / .cxx / etc. the file is included in the source file, it is very confusing and should be avoided until a real need arises. Sometimes a specific suffix (such as .tpl) is used for such files to avoid confusion for the reader.

+1
source

Source: https://habr.com/ru/post/1201062/


All Articles