How do the #endif #if #ifdef statements work?

2

At the moment of creating a project in CodeBlocks, a sentence #endif always appears in the bottom. What does this mean? What function can I give to this?

    
asked by Marco Leslie 27.11.2017 в 15:57
source

2 answers

4

That's what they're called Preprocessor Directives , in this case they would be direct conditional inclusion directives .

According to the documentation:

  

These directives allow you to include or discard part of the code of a   program if a certain condition is met.

     

#ifdef allows you to compile a section of a program only if the macro specified as a parameter has been defined, no matter which   be its value.

For example let's say you have an application and you would like it when you are developing, always start with a specific user. In this case, these directives can help you:

#define PRUEBA


#ifdef PRUEBA
  // CODIGO PARA CARGAR USUARIO POR DEFECTO
#endif

The code within the directive will only be compiled if PRUEBA is defined. So when you go to publish your application, you only have to eliminate the definition of PRUEBA and the program will follow its normal flow.

    
answered by 27.11.2017 / 16:11
source
2

As discussed Einer directives preprocessor #endif %%% and #if code% (and #ifdef , #ifndef , #elif and #else that you did not mention) are conditional code inclusion directives.

These directives are not exclusive to C ++, they are also used in .

Translation phases.

The code compilation process is divided into 9 phases in C ++, roughly:

  • It is verified that all the characters of the code are usable.
  • Separate lines are joined by descending bar ( #endif ).
  • The content of the code is classified into comments, spaces and pre-processor directives; the comments are replaced by a space ( \ ).
  • The pre-processor is running.
  • Text string literals are processed (the escape sequences are replaced by the corresponding character).
  • Adjacent text literals are concatenated.
  • The code is syntactically and semantically analyzed and compiled.
  • The translation units are examined to list the template instances required in each.
  • The translation units are united to generate the final program.
  • What does phase 4 do?

    In phase 4 the pre-processor is running, all macro are replaced by the defined value, the content of the files included by #define is copy-pasted at the point where the policy is displayed ( it by applying the phases 1 to 4 recursively) and the code that does not meet the conditions of conditional inclusion ( #include , #endif , #if , #ifdef , #ifndef , #elif and #else ) is removed.

    Suppose we have these files:

    #endif
    #ifndef OBJETO_HPP
    #define OBJETO_HPP
    
    struct Objeto {
        int a, e, i, o, u;
        const bool debug =
    #ifdef _DEBUG
        true;
    #else
        false;
    #endif
    };
    
    #endif
    
    Objeto.hpp
    #include "Objeto.hpp"
    #include "Objeto.hpp"
    
    int main()
    {
        return 0;
    }
    

    The file main.cpp will suffer the following transformations when compiled (in the example we will compile it with main.cpp defined):

  • Resolve inclusions ( _DEBUG ).

    #ifndef OBJETO_HPP
    #define OBJETO_HPP
    
    struct Objeto {
        int a, e, i, o, u;
        const bool debug =
    #ifdef _DEBUG
        true;
    #else
        false;
    #endif
    };
    
    #endif
    #ifndef OBJETO_HPP
    #define OBJETO_HPP
    
    struct Objeto {
        int a, e, i, o, u;
        const bool debug =
    #ifdef _DEBUG
        true;
    #else
        false;
    #endif
    };
    
    #endif
    
    int main()
    {
        return 0;
    }
    
  • Delete the code not included conditionally ( #include "archivo" #endif % #if , #ifdef #ifndef %% #elif and #else ).

    #ifndef OBJETO_HPP
    #define OBJETO_HPP
    
    struct Objeto {
        int a, e, i, o, u;
        const bool debug =
    #ifdef _DEBUG
        true;
    #else
    
    #endif
    };
    
    #endif
    #ifndef OBJETO_HPP
    
    
    
    
    
    
    
    
    
    
    
    
    #endif
    
    int main()
    {
        return 0;
    }
    
  • Eliminate pre-processor directives (everything that starts with #endif )

    struct Objeto {
        int a, e, i, o, u;
        const bool debug =
    
        true;
    
    
    
    };
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    int main()
    {
        return 0;
    }
    
  • The pre-processor is not the compiler.

    The compiler will receive the code files after the pre-processor has digested them, both routines are independent and do not interact. This is important since it implies that you can not use anything from the pre-processor in C ++ or anything from C ++ in the preprocessor:

    #define CONCUPISCENCIA
    
    bool funcion()
    {
    //      vvvvvvvvvvvvvv <--- No funciona!!
        if (CONCUPISCENCIA)
            return false;
        else
            return true;
    }
    
    //  vvvvvvvvv <--- No funciona
    #if funcion()
    int main()
    #else
    int WinMain()
    #endif
    {
        return 0;
    }
    

    In the previous example # is a pre-processor symbol so it can not be used in a CONCUPISCENCIA of C ++ since the language does not know of its existence. On the other hand the function if belongs to C ++ so the pre-processor can not interpret it for conditional inclusion.

        
    answered by 28.11.2017 в 17:55