C语言-程序是一连串的函数定义(function-definitions)和声明(declarations)

程序是一连串的 函数定义(function-definitions)或声明(declarations)

文法: 

translation-unit:

        external-declaration

        translation-unit external-declaration

external-declaration:

            function-definition

            declaration

 

As discussed in 5.1.1.1, the unit of program text after preprocessing is a translation unit, which

consists of a sequence of external declarations. These are described as "external" because they appear outside any function (and hence have file scope). As discussed in 6.7, a declaration that also causes storage to be reserved for an object or a function named by the identifier is a definition. 

预处理后的就是翻译单元, 一个翻译单元 就是 一串的 external declarations 。

这些 declarations 之所以称为是 external 的,是因为它们出现在所有函数的外面(即具有 file 作用域)。

根据 6.7 的讨论,由一个标识符命名的对象和函数的 declaration,引起了存储空间预留,这个 declaration 就是 definition

 

An external definition is an external declaration that is also a definition of a function (other than an inline definition) or an object. If an identifier declared with external linkage is used in an expression (other than as part of the operand of a sizeof or _Alignof operator whose result is an integer constant), somewhere in the entire program there shall be exactly one external definition for the identifier; otherwise, there shall be no more than one.

一个 external definition 就是一个 定义了函数或者对象的external declaration(除了 inline definition)

如果一个 external linkage 的标识符 出现在表达式中,整个程序的某个地方应该存在一个 这个标识符的external definition ;相反,最多 只能有 1 个(这样,具有external linkage 的标识符不用在表达式中,不需要这个标识符的 external definition)。

 

function definition 函数定义

function-definition:

        declaration-specifiers  declarator  compound-statemen

备注:

 C 11 标准中,定义的 function-definition 为

 function-definition:

          declaration-specifiers  declarator  declaration-list opt  compound-statement

 declaration-list:

              declaration

               declaration-list declaration

 

参考gcc  对C语言的实现

https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Function-Declarations

 

gcc 不支持 declarator 后面跟 declaration-list 。即对于使用gcc而言,函数定义为:

function-definition:

          declaration-specifiers  declarator  compound-statement

 

示例:

int add(int a, int b)

{

    return a+b;

}

 

The declarator in a function definition specifies the name of the function being defined and the identifiers of its parameters.

declarator 指定了函数的名称 和 参数。

 

6.7 declaration 声明

declaration:

          declaration-specifiers  init-declarator-list opt ;

          static_asser-declaration

 

声明 包含 3 部分

1、 声明指定  declaration-specifiers

2、初始声明符  init-declarator-list (可以省略)

3、分号 ;

注: 注意到这个分号了吧,这就是 C 语言中,好多地方都需要 以 分号 结尾的原因。 

declaration-specifiers:

             storage-class-specifier declaration-specifiersopt

              type-specifier declaration-specifiersopt

              type-qualifier declaration-specifiersopt

              function-specifier declaration-specifiersopt

               alignment-specifier declaration-specifiersopt

声明指示 包含    存储指定 (storage-class)    类型指定(type specifier )    类型修饰(type qualifier)   函数指定(function specifier)     对齐指定  (alignment specifier) 

storage class 符号:  static  auto  register  _Thread_local   extern  typedef 

type qualifier  符号: const  restrict    volatile   _Atomic

function specifier 符号  inline         _Noreturn

alignment specifier 符号:   _Alignas   

比较复杂的是  type specifier ,它又可以分为:

 6.7.2  type-specifier:

              void

              char

              short

              int

             long

             float

            double

            signed

            unsigned

          _Bool

           _Complex

             atomic-type-specifier 

             struct-or-union-specifier

             enum-specifier

上面的  char void  等类型都好理解。下面的  有 三个类型  atomic type   ; struct and union type  ;  enum type 又具有各自的文法。

atomic-type-specifier:

           _Atomic ( type-name )

              type-name 不能是   数组类型; 函数类型  atomic 类型; 修饰后的类型

enum-specifier:

              enum identifieropt { enumerator-list }

              enum identifieropt { enumerator-list , }

              enum identifier

enumerator-list:

             enumerator

              enumerator-list , enumerator

enumerator:

            enumeration-constant

             enumeration-constant = constant-expression

 

枚举类型  -  示例

enum weekday { monday, tuesday, }   //【注:这儿没有 分号,因为 只举例 一个  enum specifier  而不是举例 declaration。 这儿一旦加上 分号,即构成  type specifier + ; 组成 declaration 】

 

 

struct-or-union-specifier:

              struct-or-union identifieropt { struct-declaration-list }

              struct-or-union identifier

struct-or-union:

             struct

             union

struct-declaration-list:

             struct-declaration

             struct-declaration-list struct-declaration

struct-declaration:

              specifier-qualifier-list struct-declarator-listopt ;  [注释,注意这儿的 分号]

              static_assert-declaration

specifier-qualifier-list:

              type-specifier     specifier-qualifier-listopt

              type-qualifier   specifier-qualifier-listopt

               alignment-specifier specifier-qualifier-listopt

struct-declarator-list:

            struct-declarator

              struct-declarator-list , struct-declarator  [注: 注意中间的 逗号]

struct-declarator:

            declarator

            declaratoropt : constant-expression  【bit field 定义时使用】

示例:

typedef struct
{
    unsigned long byte_half : 4;             //0.5      【注: 这儿的分号,来自于 struct-declaration 最后的分号】
    unsigned long byte_oneAndHalf : 12;      //2
    union
    {
        unsigned long byte_union_one_1 : 8;  //3
        unsigned long byte_union_one_2 : 8;  //3
        unsigned long byte_union_one_3 : 8;  //3
    };
    unsigned long byte_one        : 8;       //4
}LongStruct  ; [注释: 这儿的 分号,来自于 declaration 里面的最后的分号]

 

posted @ 2022-01-08 16:59  张志伟122  阅读(349)  评论(0编辑  收藏  举报