Hello every one I want to ask a question about include guards in C programming.I know there purpose but in some codes I have seen "1" written after #define like
#ifndef MYFILE_H
#define MYFILE_H 1
What is the purpose of this "1" ? Is it necessary ? Thanks
It's not necessary,
#define MYFILE_H
should do the trick. The fact thatMYFILE_H
is defined (the condition tested byifndef
) is separated from its value. It could be 0, ' ', 42, etc.It's a style thing, as far as i know. That '1' is unnecessary in my opinion; it doesn't really do anything.
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:then the
1
is not required in the macro definition-but it if it used elsewhere with an
#if
directive like here: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.