非常教程

C参考手册

C 语法

Lookup and name spaces

当在C程序中遇到标识符时,执行查找以找到引入该标识符的声明,该声明当前处于范围内。如果这些标识符属于不同的类别(称为名称空间),那么C允许同一标识符的多个声明同时处于同一范围内:

1)标签名称空间:将所有标识符声明为标签。

2)标签名称:声明为结构名称,联合和枚举类型的所有标识符。请注意,所有三种标签共享一个名称空间。

3)成员名称:声明为任何一个结构或联合的成员的所有标识符。每个结构和联合都引入了它自己的这种名称空间。

4)所有其他标识符,称为普通标识符以区分(1-3)(函数名称,对象名称,typedef名称,枚举常量)。

在查找时,标识符的名称空间由其使用方式决定:

1)在标签名称空间中查找作为goto语句的操作数出现的标识符。

2)随后的关键字标识符structunionenum在标签名称空间中查找。

3)通过指针操作符访问成员访问或成员访问之后的标识符在由成员访问操作符的左操作数确定的类型的成员的名称空间中查找。

4)在普通标识符的名称空间中查找所有其他标识符。

笔记

宏的名称不是任何名称空间的一部分,因为它们在语义分析之前被预处理器替换。

通常的做法是使用typedef声明将struct / union / enum名称注入普通标识符的名称空间中:

struct A { };       // introduces the name A in tag name space
typedef struct A A; // first, lookup for A after "struct" finds one in tag name space
                    // then introduces the name A in the ordinary name space
struct A* p;        // OK, this A is looked up in the tag name space
A* q;               // OK, this A is looked up in the ordinary name space

跨两个名称空间使用相同标识符的着名示例是statPOSIX标头中的标识符sys/stat.h。它用作普通标识符时指定一个函数,并在用作标记时指示一个结构体。

与C ++不同,枚举常量不是结构成员,它们的名称空间是普通标识符的名称空间,并且由于C中没有结构作用域,因此它们的作用域是结构声明出现的范围:

struct tagged_union 
{
   enum {INT, FLOAT, STRING} type;
   int integer;
   float floating_point;
   char *string;
} tu;
 
tu.type = INT; // OK in C, error in C++

void foo (void) { return; } // ordinary name space, file scope
struct foo {      // tag name space, file scope
    int foo;      // member name space for this struct foo, file scope
    enum bar {    // tag name space, file scope
        RED       // ordinary name space, file scope
    } bar;        // member name space for this struct foo, file scope
    struct foo* p; // OK: uses tag/file scope name "foo"
};
enum bar x; // OK: uses tag/file-scope bar
// int foo; // Error: ordinary name space foo already in scope 
//union foo { int a, b; }; // Error: tag name space foo in scope
 
int main(void)
{
    goto foo; // OK uses "foo" from label name space/function scope
 
    struct foo { // tag name space, block scope (hides file scope)
       enum bar x; // OK, uses "bar" from tag name space/file scope
    };
    typedef struct foo foo; // OK: uses foo from tag name space/block scope
                            // defines block-scope ordinary foo (hides file scope)
    (foo){.x=RED}; // uses ordinary/block-scope foo and ordinary/file-scope RED
 
foo:; // label name space, function scope
}

参考

  • C11 standard (ISO/IEC 9899:2011):
    • 6.2.3 Name spaces of identifiers (p: 37)
  • C99 standard (ISO/IEC 9899:1999):
    • 6.2.3 Name spaces of identifiers (p: 31)
  • C89/C90 standard (ISO/IEC 9899:1990):
    • 3.1.2.3 Name spaces of identifiers

C 语法相关

1.#define directive
2.#elif directive
3.#else directive
4.#endif directive
5.#error directive
6.#if directive
7.#ifdef directive
8.#ifndef directive
9.#include directive
10.#line directive
11.#pragma directive
12.alignas
13.Alternative operators and tokens
14.Analyzability
15.Arithmetic operators
16.Arithmetic types
17.Array declaration
18.Array initialization
19.ASCII Chart
20.Assignment operators
21. types
22.Basic concepts
23.Bit fields
24.break statement
25.C language
26.C Operator Precedence
27.cast operator
28.character constant
29.Comments
30.Comparison operators
31.compound literals
32.Conditional inclusion
33.Conformance
34.const type qualifier
35.Constant expressions
36.continue statement
37.Declarations
38.do-while loop
39.Enumerations
40.Escape sequences
41.Expressions
42.External and tentative definitions
43.File scope
44.floating constant
45.for loop
46.Function declarations
47.Function definitions
48.Functions
49.Generic selection
50.goto statement
51.Identifier
52.if statement
53.Implicit conversions
54.Increment/decrement operators
55.Initialization
56.inline function specifier
57.integer constant
58.Lifetime
59.Logical operators
60.Main function
61.Member access operators
62.Memory model
63.Objects and alignment
64.Order of evaluation
65.Other operators
66.Phases of translation
67.Pointer declaration
68.Preprocessor
69.restrict type qualifier
70.return statement
71.Scalar initialization
72.Scope
73.sizeof operator
74.Statements
75.static assert declaration
76.Static storage duration
77.Storage-class specifiers
78.string literals
79.Struct and union initialization
80.Struct declaration
81.switch statement
82.Thread storage duration
83.Type
84.Type
85.Typedef declaration
86.Undefined behavior
87.Union declaration
88.Value categories
89.Variadic arguments
90.volatile type qualifier
91.while loop
92._Alignof operator
93._Noreturn function specifier
C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。