Search code examples
cinclude

Include Guards syntax in C


Hello everyone I want to ask a question about including guards in C programming. I know their purpose but in some programms I have seen a 1" written after #define like this:

#ifndef MYFILE_H
#define MYFILE_H 1

What is the purpose of this 1? Is it necessary?


Solution

  • It is not necessary if the MYFILE_H macro is not used elsewhere in your code.

    If it is used elsewhere with an #ifdef or #ifndef directive like here:

    #ifdef MYFILE_H 
    

    then the 1 is not required in the macro definition-

    but it if it used elsewhere with an #if directive like here:

    #if MYFILE_H
    

    then the 1 (or any value != 0) is required in the macro definition.

    Note these directives could be used in a source file to verify if the header is included or not.