Why headerFileName_H

While I am creating a C ++ header file, I declare a header file, for example:

/*--- Pencere.h ---*/ #ifndef PENCERE_H #define PENCERE_H 

I want to find out why I need to write underscores.

+4
source share
4 answers

You do not need to use underscore, this is just an agreement to separate the header name and extension. You cannot use a literal . , since this is not valid in the identifier, so you replace it with an underscore that is valid.

The reason you actually do this is to turn on the guard. The entire contents of the file look something like this:

 #ifndef PENCERE_H #define PENCERE_H // Your stuff goes here. #endif 

so that if you accidentally turn it on twice:

 #include "pencere.h" #include "pencere.h" 

you will not get everything that is duplicated in it. Double inclusions are usually finer than this, for example, you can include pax.h and diablo.h in your code, and pax.h also includes diablo.h for your purposes:

 main.c: #include "pax.h" #include "diablo.h" // Other stuff pax.h: #ifndef PAX_H #define PAX_H #include "diablo.h" // Other stuff #endif diablo.h: #ifndef DIABLO_H #define DIABLO_H typedef int mytype; #endif 

In this case, if there were no guards enabled, try compiling the string typedef int mytype; twice in your program. Once for main.c -> pax.h -> diablo.h and again for main.c -> diablo.h .

With guards turned on, the pre-processor DIABLO_H is defined when main.c turns on diablo.h , so #define and typedef not processed.

This particular mapping of header files to #define names breaks down in a situation where you have dir1/pax.h and dir2/pax.h , since they will use PAX_H . In this case, to solve the problem, you can use a circuit of the type DIR1_PAX_H and DIR2_PAX_H .

+6
source

Underscore is just a way of creating a line for an included defender that is unlikely to be created anywhere else and will make it difficult to detect problems. Moreover, you can select any character for the inclusion protector if it is not defined elsewhere.

+4
source

This is because you cannot #define PENCERE.H

You can define whatever you want, but using the file name use format by replacing . the _ means that you should not encounter C # defines, which protect the import of the same header file twice.

+2
source

You do not need to underline. All you need is a preprocessor symbol that is not defined anywhere else. If you like (and / or if you have Pascal background; -}) you can just say

 /*--- Pencere.h ---*/ #ifndef THE_PENCERE_HEADER_FILE_WAS_INCLUDED #define THE_PENCERE_HEADER_FILE_WAS_INCLUDED 
+1
source

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


All Articles