C Header include guards are used only in header files, but not in .c files. What for?

I have been using protection in header files for a while, and the only reason I understand why they are used is to enable one inclusion of this (header file with protection in mind) at compile time.

I want to know if there are other reasons for using header protectors and why they are not used in .c files and what happens if guards are also used for .c files?

Repl. collected from the answers below.

Typically, all definitions are included in a .c file, and header files (.h files) include all declarations. It is not a good practice to include .c files.

To link only one inclusion of declarations made available for the .c file at compile time, which is specific to the translation unit (therefore, if there are two or more libraries that need to be linked, i.e. we have two or more translation units) ; header protectors help to include the header file ONLY ONCE.

This is due to the preprocessor stage before the files are compiled to receive the file (.o extension). The preprocessor stage replaces all macros and includes the corresponding data that allows you to include your .h file ONLY once.

+4
source share
2 answers

, , , .

, , , a.h b.h. b.h a.h, a.h b.h s.c. a.h , . , a.h .

, .

+11

, , .

example_1.h
example_2.h (includes example_1.h)

'C' 'example_1.h' 'example_2.h' ()

'#include example_1.h' .

#ifdef EXAMPLE_1
#define EXAMPLE_1


#endif

to example_1.h, , , #include example_1.h, ( ), , example_1. h. , #include example_1.h , EXAMPLE_1 .

, , . .c files , .c files.

+2

All Articles