简体   繁体   English

在 C 中包含 Guards 语法

[英]Include Guards syntax in C

Hello everyone I want to ask a question about including guards in C programming.大家好,我想问一个关于在 C 编程中包含守卫的问题。 I know their purpose but in some programms I have seen a 1 " written after #define like this:我知道他们的目的,但在某些程序中,我看到1 " 写在 #define 之后,如下所示:

#ifndef MYFILE_H
#define MYFILE_H 1

What is the purpose of this 1 ?这个1的目的是什么? Is it necessary?有必要吗?

It's not necessary, #define MYFILE_H should do the trick.没有必要, #define MYFILE_H应该可以解决问题。 The fact that MYFILE_H is defined (the condition tested by ifndef ) is separated from its value.定义MYFILE_H的事实(由ifndef测试的条件)与其值分开。 It could be 0, ' ', 42, etc.它可以是 0、' '、42 等。

It is not necessary if the MYFILE_H macro is not used elsewhere in your code.如果MYFILE_H宏未在代码中的其他地方使用,则没有必要。

If it is used elsewhere with an #ifdef or #ifndef directive like here:如果它在其他地方与#ifdef#ifndef指令一起使用,如下所示:

#ifdef MYFILE_H 

then the 1 is not required in the macro definition-那么宏定义中不需要1

but it if it used elsewhere with an #if directive like here:但如果它在其他地方使用#if指令,如这里:

#if MYFILE_H

then the 1 (or any value != 0 ) is required in the macro definition.那么宏定义中需要1 (或任何值!= 0 )。

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;在我看来,“1”是不必要的; it doesn't really do anything.它真的没有做任何事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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