Include Guards syntax in C

2019-09-19 04:49发布

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

标签: c include
3条回答
小情绪 Triste *
2楼-- · 2019-09-19 05:35

It's not necessary, #define MYFILE_H should do the trick. The fact that MYFILE_H is defined (the condition tested by ifndef) is separated from its value. It could be 0, ' ', 42, etc.

查看更多
对你真心纯属浪费
3楼-- · 2019-09-19 05:37

It's a style thing, as far as i know. That '1' is unnecessary in my opinion; it doesn't really do anything.

查看更多
神经病院院长
4楼-- · 2019-09-19 05:51

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.

查看更多
登录 后发表回答