什么是双下划线(__const c)中是什么意思? [英] What does double underscore ( __const) mean in C?

查看:343
本文介绍了什么是双下划线(__const c)中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

extern int ether_hostton (__const char *__hostname, struct ether_addr *__addr)
 __THROW;

我发现/usr/include/netinet/ether.h以上函数的定义在Linux机器上。

I found the above function definition in /usr/include/netinet/ether.h on a Linux box.

有人能解释一下什么是双下划线的常量(关键字),地址(标识)的前面,最后__THROW的意思。

Can someone explain what the double underscores mean in front of const (keyword), addr (identifier) and at last __THROW.

推荐答案

在C,符号开始以下划线后跟一个大写字母或另一个下划线被保留用于实施。你为C的用户不应该创建与保留序列启动任何符号。在C ++中,限制更为严格;你的用户可能无法创建一个符号包含一个双下划线。

In C, symbols starting with an underscore followed by either an upper-case letter or another underscore are reserved for the implementation. You as a user of C should not create any symbols that start with the reserved sequences. In C++, the restriction is more stringent; you the user may not create a symbol containing a double-underscore.

假设:

extern int ether_hostton (__const char *__hostname, struct ether_addr *__addr)
__THROW;

__常量记法有允许的可能性(有点不太可能)说,这code被用于支持原型符号,但没有一个编译器在C89标准关键字的正确理解常量。在的autoconf 宏还可以检查编译器是否有常量工作的支持;这code可以用一个破碎的编译器不具有支持使用。

The __const notation is there to allow for the possibility (somewhat unlikely) that a compiler that this code is used with supports prototype notations but does not have a correct understanding of the C89 standard keyword const. The autoconf macros can still check whether the compiler has working support for const; this code could be used with a broken compiler that does not have that support.

__主机使用 __地址对你是一个保护措施,头的用户。如果你使用GCC和 -Wshadow 选项编译,编译器会发出警告,当任何局部变量阴影全局变量。如果函数使用刚刚主机名而不是 __主机,如果你有一个名为函数主机名(),有会是一个阴影。通过使用保留实施的名字,则您的合法code没有冲突。

The use of __hostname and __addr is a protection measure for you, the user of the header. If you compile with GCC and the -Wshadow option, the compiler will warn you when any local variables shadow a global variable. If the function used just hostname instead of __hostname, and if you had a function called hostname(), there'd be a shadowing. By using names reserved to the implementation, there is no conflict with your legitimate code.

使用的__ THROW 表示code能,在某些情况下,可以用某种'扔规范的声明。这不是标准C;它更像是C ++。但是,code可以用C语言编译器,只要头一个(或编译器本身)定义 __ THROW 空使用,或者一些编译器特定的扩展标准C语法。

The use of __THROW means that the code can, under some circumstances, be declared with some sort of 'throw specification'. This is not standard C; it is more like C++. But the code can be used with a C compiler as long as one of the headers (or the compiler itself) defines __THROW to empty, or to some compiler-specific extension of the standard C syntax.


C标准的第7.1.3节(ISO 9899:1999)说:

Section 7.1.3 of the C standard (ISO 9899:1999) says:

每个标题声明或定义在其相关的上市节所有标识符,并
  可选声明或定义在其相关联的未来图书馆方向上市标识符
  款和它们总是保留要么为任何用途或用作文件识别符
  范围标识符。

7.1.3 Reserved identifiers

Each header declares or defines all identifiers listed in its associated subclause, and optionally declares or defines identifiers listed in its associated future library directions subclause and identifiers which are always reserved either for any use or for use as file scope identifiers.

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

— 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.

- 任一下列条款的每一个宏名(包括未来图书馆
  方向)被保留用于如果任何相关标头包括按照指定;
  除非另有明确规定(见7.1.4)。

— Each macro name in any of the following subclauses (including the future library directions) is reserved for use as specified if any of its associated headers is included; unless explicitly stated otherwise (see 7.1.4).

- 在任何以下小节的外部连接(包括所有的标识符
  未来图书馆方向)总是保留用于与外部标识符
  联动。 154)

— All identifiers with external linkage in any of the following subclauses (including the future library directions) are always reserved for use as identifiers with external linkage.154)

- 在任何以下小节上市文件范围内每个标识符(包括
  未来图书馆方向)被保留用于为宏名称和与标识符
  如果任何相关报头被包括在相同的名称空间的文件范围。

— Each identifier with file scope listed in any of the following subclauses (including the future library directions) is reserved for use as a macro name and as an identifier with file scope in the same name space if any of its associated headers is included.

没有其它标识符是保留。如果程序声明或在一个限定的标识符
  上下文中它被保留(比其它由7.1.4所允许),或限定一个保留
  标识为宏名,行为是不确定的。

No other identifiers are reserved. If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined.

如果该程序删除(以和#undef )的标识符的第一任宏定义
  以上所列组,该行为是不确定的。

If the program removes (with #undef) any macro definition of an identifier in the first group listed above, the behavior is undefined.

脚注154)与外部链接保留标识符的列表包括错误号 math_errhandling
  的setjmp va_end用来

Footnote 154) The list of reserved identifiers with external linkage includes errno, math_errhandling, setjmp, and va_end.


又见<一个href=\"http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier/228797#228797\">What大约用一个C ++标识符下划线的规则;很多相同的规则适用于C和C ++,虽然嵌入式双下划线的规则是在C ++中只,截至这个答案的上方提到的。

See also What are the rules about using an underscore in a C++ identifier; a lot of the same rules apply to both C and C++, though the embedded double-underscore rule is in C++ only, as mentioned at the top of this answer.

这篇关于什么是双下划线(__const c)中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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