For which situations can this definition be useful?

#ifndef __IMAGE_H__ #define __IMAGE_H__ //////////////////// #endif 
  • 2
    so that the code inside is not executed twice, if the file in which it is defined is connected several times, for example - Grundy

3 answers 3

These directives are designed to protect against multiple inclusion in the presence of a complex hierarchy of included files - if such a file has already been included somewhere before, then __IMAGE_H__ will be declared, which means that the conditional #define will be skipped, along with all declarations before #endif

Some compilers have similar #pragma actions, for example,

 #pragma once 

But the conditional version is the most universal.

  • one
    All popular ones have #pragma once , so de facto this can be considered standard functionality - ixSci

This is the same classic include guard , which protects against re-including header files so that the same functions / classes are not overridden.

    This is usually done in header files included by include . If the header file is included in the program twice, it will cause a bunch of "re-announcement" errors. To prevent this from happening, this define declares the flag that the file is already included and therefore, when it is re-enabled, ifndef will not work and the entire contents of the file will be ignored.

    • Not "in the program", but in the same broadcast unit. Header files are intended for multiple inclusion in the program and, of course, do not need to be protected from it. - AnT