Preprocessor #ifndef

后端 未结 3 1185
星月不相逢
星月不相逢 2021-01-17 02:54

Assume I have a.h which includes the following:




Assume I also have b.

3条回答
  •  时光说笑
    2021-01-17 03:30

    If stdbool.h itself has include guards (#ifndef) then everything will be fine. Otherwise you may indeed end up including some headers twice. Will it cause a problem? It depends. If the twice-included header contains only declarations then everything will compile - it will just take few nanoseconds longer. Imagine this:

    int the_answer(void); // <-- from first inclusion
    int the_answer(void); // <-- from from second inclusion - this is OK
                          //       at least as long as declarations are the same
    
    int main()
    {
        return the_answer();
    }
    

    If on the other hand there will be definitions it will cause an error:

    int the_answer(void)  // <-- from first inclusion - OK so far
    {
        return 42;
    }
    
    int the_answer(void)  // <-- from second inclusion
    {                     //     error: redefinition of 'the_answer'
        return 42;
    }
    
    int main()
    {
        return the_answer();
    }
    

提交回复
热议问题