简体   繁体   中英

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?

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM