命名空间Name Spaces

编译器设置“命名空间”来区分用于各种项的标识符。The compiler sets up "name spaces" to distinguish between the identifiers used for different kinds of items. 每个命名空间中的名称必须是唯一的以避免冲突,但相同的名称可出现在多个命名空间中。The names within each name space must be unique to avoid conflict, but an identical name can appear in more than one name space. 这意味着,可以对两个或更多不同的项使用同一个标识符,前提是这些项位于不同的命名空间中。This means that you can use the same identifier for two or more different items, provided that the items are in different name spaces. 编译器可以基于程序中标识符的语义上下文来解析引用。The compiler can resolve references based on the syntactic context of the identifier in the program.

备注

不要将命名空间的有限 C 概念与 C++“命名空间”功能混淆。Do not confuse the limited C notion of a name space with the C++ "namespace" feature. 有关详细信息,请参阅《C++ 语言参考》中的命名空间See Namespaces in the C++ Language Reference for more information.

此列表描述了 C 中使用的命名空间。This list describes the name spaces used in C.

语句标签Statement labels
命名的语句标签是语句的一部分。Named statement labels are part of statements. 语句标签的定义始终后跟冒号,但不是 case 标签的一部分。Definitions of statement labels are always followed by a colon but are not part of case labels. 语句标签的使用始终后跟关键字 gotoUses of statement labels always immediately follow the keyword goto. 语句标签不必与其他名称或其他函数中的标签名称有所不同。Statement labels do not have to be distinct from other names or from label names in other functions.

结构、联合和枚举标记Structure, union, and enumeration tags
这些标记是结构、联合和枚举类型说明符的一部分,总是紧跟在保留字 structunionenum 的后面(如果有)。These tags are part of structure, union, and enumeration type specifiers and, if present, always immediately follow the reserved words struct, union, or enum. 标记名称必须不同于具有相同可见性的所有其他结构、枚举或联合标记。The tag names must be distinct from all other structure, enumeration, or union tags with the same visibility.

结构或联合的成员Members of structures or unions
成员名称分配在与每个结构和联合类型关联的命名空间中。Member names are allocated in name spaces associated with each structure and union type. 即,同一标识符可以同时为任意数量的结构或联合的组件名称。That is, the same identifier can be a component name in any number of structures or unions at the same time. 组件名称的定义总是出现在结构或联合类型说明符中。Definitions of component names always occur within structure or union type specifiers. 组件名称的使用总是紧跟在成员选择运算符(->.)之后。Uses of component names always immediately follow the member-selection operators (-> and .). 成员的名称在结构或联合中必须是唯一的,但它无需不同于程序中的其他名称(包括不同的结构和联合的成员或结构本身的名称)。The name of a member must be unique within the structure or union, but it does not have to be distinct from other names in the program, including the names of members of different structures and unions, or the name of the structure itself.

普通标识符Ordinary identifiers
所有其他名称都属于一个包含变量、函数(包括形参和局部变量)和枚举常量的命名空间。All other names fall into a name space that includes variables, functions (including formal parameters and local variables), and enumeration constants. 标识符名称具有嵌套可见性,因此您可以在块内重新定义它们。Identifier names have nested visibility, so you can redefine them within blocks.

Typedef 名称Typedef names
Typedef 名称不能用作同一范围内的标识符。Typedef names cannot be used as identifiers in the same scope.

例如,由于结构标记、结构成员和变量名位于三个不同的命名空间中,因此该示例中名为 student 的三个项不会发生冲突。For example, since structure tags, structure members, and variable names are in three different name spaces, the three items named student in this example do not conflict. 在该程序中,每个项的上下文允许对 student 的每个匹配项进行正确解释。The context of each item allows correct interpretation of each occurrence of student in the program. (有关结构的信息,请参阅结构声明。)(For information about structures, see Structure Declarations.)

struct student {  
   char student[20];  
   int class;  
   int id;  
   } student;  

student 出现在 struct 关键字的后面时,编译器会将其识别为结构标记。When student appears after the struct keyword, the compiler recognizes it as a structure tag. student 出现在成员选择运算符 (->.) 的后面时,名称将引用结构成员。When student appears after a member-selection operator (-> or .), the name refers to the structure member. 在其他上下文中,student 引用结构变量。In other contexts, student refers to the structure variable. 但是,建议不要重载标记命名空间,因为它会使含义变得模糊。However, overloading the tag name space is not recommended since it obscures meaning.

另请参阅See Also

程序结构Program Structure