Preprocessor #ifndef

淺唱寂寞╮ 提交于 2019-12-01 13:51:18

All C standard headers must be made such that they can be included several times and in any order:

Standard headers may be included in any order; each may be included more than once in a given scope, with no effect different from being included only once

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();
}

It's normal that most header start with

#ifndef _HEADERFILENAME_H_
#define _HEADERFILENAME_H_

and end with the following line:

#endif

If you include a header two times, the second time your programm won't include the full header again because of the #ifndef, #define and #endif

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!