了解c中#defines的不同样式 [英] Understanding different styles of #defines in c

查看:79
本文介绍了了解c中#defines的不同样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C编程语言查看不同的源代码,发现#define的使用了不同样式。

I was looking at different source codes in C programming language and found different styles of #define's used.

我从技术上知道它们的用例,而不论它们的样式如何可以按预期工作,但是根据编码准则,我想问一下这是什么意思,以及何时使用特定的代码。

I know technically their use case and irrespective of their style they will work as expected but from coding guidelines, I would like to ask what do they mean and when to use a particular one.

#define MY_DEFINE_H

#define MY_DEFINE_H_

#define MY_DEFINE_H__

#define __MY_DEFINE_H__

如果可能的话,也请分享一些参考,以便我详细介绍。

Also if possible please share some reference so that I can go through detailed in it.

推荐答案

请注意,通常,您不应创建以下划线开头的函数,变量,标记或宏名称。 C11§7.1.3保留标识符的一部分说:

Note that you should not, in general, create function, variable, tag or macro names that start with an underscore. Part of C11 §7.1.3 Reserved identifiers says:


  • 所有以下划线开头,大写字母或其他下划线开头的标识符始终保留用于任何用途。

  • 所有以下划线开头的标识符始终保留用作普通和标记名称空间中文件范围的标识符。

  • All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
  • All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.

另请参见什么是双下划线( __ const )用C表示吗?

See also What does double underscore (__const) mean in C?

这表示姓氏( __ MY_HEADER_H __ )可以由系统标头使用(其他标头不能由系统标头使用)。请注意,一个常见的问题是新程序员希望查看系统头文件并复制它们,而不是意识到实现提供的头文件的规则(我称为系统头文件)要遵循与编写的头文件不同的规则由用户。因此,人们不经意间践踏了系统名称空间,认为这是一个好主意,因为这是系统头文件所做的事情,而没有意识到他们必须不要这样做,以便可以安全地编写系统头文件。

That means the last name (__MY_HEADER_H__) can be used by 'system' headers (and the others can't be used by system headers). Note that a common problem is that new programmers look to see what the system headers do and copy them, not realizing that the rules for the headers provided by 'the implementation' (what I called system headers) are subject to different rules from headers written by users. Consequently, people inadvertantly trample on the system namespace thinking it is a good idea because that's what the system headers do, not realizing that they must not do it so that the system headers can be written safely.

从技术上讲,您可以自己使用其他三个名称中的任何一个。我不喜欢结尾的下划线,因此在没有令人信服的理由的情况下不使用它们。这些标头防护程序是否可以防止出现多个夹杂物?

Technically, you can use any of the other three names yourself. I don't like the trailing underscores so I don't use them in the absence of a compelling reason. Are these header guards to prevent multiple inclusions?

#ifndef MY_HEADER_H
#define MY_HEADER_H
…
#endif /* MY_HEADER_H */

如果名称用于标题保护,则使用单个或双下划线表示他们不太可能与其他名称发生冲突。您不太可能引用这些宏。您应该抵制尝试写入其他源文件的诱惑:

If the names are for header guards, using a single or double underscore means they're less likely to collide with other names. You're not likely to refer to these macros. You should resist the temptation to try writing in some other source file:

#ifndef MY_HEADER_H__
#include "my_header.h"
#endif

标题中的名称可能会更改。标头包含一组标头保护(至关重要的例外)是至关重要的。但是标头本身之外的代码通常不应该知道该名称。

The name in the header could change. It is crucial that the header contains a set of header guards (the exceptions are rare). But code outside the header itself should not usually be aware of that name.

我倾向于使用 HEADER_H HEADER_H_INCLUDED 用于文件 header.h (而且我很少使用我的作为任何内容的前缀),但是只要名称唯一,名称就没有关系(该文件的MD5校验和可能很好-这不是安全应用程序)。

I tend to use either HEADER_H or HEADER_H_INCLUDED for file header.h (and I seldom if ever use 'my' as a prefix to anything), but the name doesn't matter as long as it is unique (an MD5 checksum for the file is likely to be fine — this isn't a security application).

这篇关于了解c中#defines的不同样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆