The point of the question seems to be summed up nicely in the summary that was edited on:
Edit: How does the preprocessor know that I am trying to prevent double include of a header file named test.h
if the name after
#ifndef
is not important and can be anything like: #ifndef Dummy
,
The preprocessor doesn't know that you are specifically trying to prevent multiple inclusion, nor anything actionable about the name of the header file you are trying to protect. This is an application of the preprocessor's general purpose conditional compilation mechanism to the specific problem of accommodating (not actually preventing) multiple inclusion.
and what's even the point of using a name if I am not going to use it
[...]
But you do use it. In the #ifndef
directive. That the macro name referenced there is the same as the one #define
d is crucial for this mechanism to work correctly.
The mechanism makes use of the fact that the preprocessor can test whether specific macro names are defined, and use the result as a condition for whether an associated section of a source file is compiled. This is what the #ifndef
... #endif
is about. The #define
ensures that compiling the section between causes the specified preprocessor macro to be defined, so that the #ifndef
condition evaluates to false on subsequent inclusions of the same header. Thus, the specific choice of macro name is not significant, but the fact that it is distinct from all the macro names used by other headers (or that it is not, if that is done purposefully) is significant.
should they make it easier and let us only type:
#ifndef
#define
Again, this is a specific application of a general-purpose mechanism. The general purpose mechanism is for the definition of preprocessor macros and the testing of whether they are defined, which are parts of the larger preprocessor macro facility. This facility works in terms of specific macro names. Moreover, even if it were sensible, an empty macro name would not work for the purpose because (if used generally) it would not satisfy the necessary criterion of each header using a distinct macro name.
However, some C implementations agree with you that header guards could be easier. As a common (but not universal!) extension, some implementations recognize
#pragma once
appearing at the beginning of a header as a substitute for conditional-compilation-based inclusion guards.