表达式(Expressions)

主表达式(Primary expressions)

语法:

说明:

  • identifier:标识符 例如:int a ; 这里 a就是标识符
  • constant: 常量 (详见c99词法部分)
  • string-literal:字符串字面值 (详见c99词法部分)

后缀运算符(Postfix operators)

语法:

  1. Array subscripting (数组下标) 手册 P6.5.2.1

  2. Function calls (函数调用) 手册 P6.5.2.2

  3. Structure and union members (结构体和联合体成员) 手册 P6.5.2.3

  4. Postfix increment and decrement operators (后缀++和— 运算符) 手册 P6.5.2.4

  5. Compound literals (复合字面值) 手册 P6.5.2.5

compound literals

compound literals 是由一个带括号的类型名和一个大括号的初始值设定项列表组成。

例子:int * p = ( int [] ){ 2 , 4 };

一元运算符 (Unary operators)

语法:

  1. Prefix increment and decrement operators(前缀++和—) 手册P6.5.3.1

  2. Address and indirection operators 手册P6.5.3.2

  3. Unary arithmetic operators (一元算数运算符) 手册P6.5.3.3

  4. The sizeof operator (sizeof运算符) 手册P6.5.3.4

Constraints(约束条件):

英文:The operand of the prefix increment or decrement operator shall have qualified or unqualified real or pointer type and shall be a modifiable lvalue.

我的理解:前缀递增或递减运算符的操作数应是有限制或无限制的实数或指针类型,并且应为可修改的左值。

Cast operators

语法:

文档P6.5.4

注:强制类型转换,在这里定义

Multiplicative operators

语法:

文档P6.5.5

Constraints(约束条件):

英文:Each of the operands shall have arithmetic type. The operands of the % operator shall have integer type

我的理解:每一个操作数都应该是算数类型,%运算符的操作数应该是整数类型。

Additive operators

语法:

文档P6.5.6

Constraints(约束条件):
  1. 两个操作数都应为算术类型,或者一个操作数应为指向对象类型的指针,另一个操作数应为整数类型

  2. 对于减法,应满足下列要求中的一项:

    • 两个操作数都是算数类型。
    • 两个操作数都是指向兼容对象类型的限定或非限定版本的指针。
    • 左操作数是指向对象类型的指针,右边的操作数具有整数类型。

Bitwise shift operators(移位运算符)

语法:

文档P6.5.7

Constraints(约束条件):

每一个操作数都应该是整数类型

Relational operators (关系运算符)

语法:

文档P6.5.8

Constraints(约束条件):

应满足下列要求中的一项:

  1. 两个操作数都是实数类型
  2. 两个操作数都是指向兼容对象类型的限定或非限定版本的指针( both operands are pointers to qualified or unqualified versions of compatible object types)
  3. both operands are pointers to qualified or unqualified versions of compatible incomplete types

Equality operators

语法:

文档P6.5.9

Constraints(约束条件):

应满足下列要求中的一项:

  1. 两个操作数都是算数类型
  2. 两个操作数都是指向兼容类型的有限制或无限制版本的指针;(both operands are pointers to qualified or unqualified versions of compatible types)
  3. one operand is a pointer to an object or incomplete type and the other is a pointer to a qualified or unqualified version of void
  4. one operand is a pointer and the other is a null pointer constant

Bitwise AND operator

语法:

文档P6.5.10

Constraints(约束条件):

每一个操作数都要求是整数类型。

Bitwise exclusive OR operator

语法:

文档P6.5.11

Constraints(约束条件):

每一个操作数都要求是整数类型。

Bitwise inclusive OR operator

语法:

文档P6.5.12

Constraints(约束条件):

每一个操作数都要求是整数类型。

Logical AND operator

语法:

文档P6.5.13

Constraints(约束条件):

每一个操作数都要求是标量类型。(Each of the operands shall have scalar type)

Logical OR operator

语法:

文档P6.5.14

Constraints(约束条件):

每一个操作数都要求是标量类型。(Each of the operands shall have scalar type)

Conditional operator (条件运算符)

语法:

文档P6.5.15

Constraints(约束条件):
  1. 第一个操作数应该是标量类型
  2. 第二个和第三个操作数应满足以下条件之一:
    • 两个操作数都具有算术类型;
    • both operands have the same structure or union type
    • both operands have void type
    • both operands are pointers to qualified or unqualified versions of compatible types
    • one operand is a pointer and the other is a null pointer constan
    • one operand is a pointer to an object or incomplete type and the other is a pointer to a qualified or unqualified version of void

Assignment operators (赋值运算符)

语法:

文档P6.5.16

Constraints(约束条件):

赋值运算符应具有可修改的左值作为其左操作数。

Comma operator

语法:

文档P6.5.17

Semantics(语法):

The left operand of a comma operator is evaluated as a void expression;there is a sequence point after its evaluation. Then the right operand is evaluated; the result has its type and value. If an attempt is made to modify the result of a comma operator or to access it after the next sequence point, the behavior is undefined.

注:还是直接看英文了,翻译水平有限,还是看英文理解比较好

EXAMPLE(例子): As indicated by the syntax, the comma operator (as described in this subclause) cannot appear in contexts where a comma is used to separate items in a list (such as arguments to functions or lists of initializers). On the other hand, it can be used within a parenthesized expression or within the second expression of a conditional operator in such contexts. In the function call $$ f(\quad a,\quad (t=3,\quad t+2),\quad c) $$ the function has three arguments, the second of which has the value 5. ## Constant expression **语法:** $$ \begin{gather*} constant\texttt{-}expression: \\ conditional\texttt{-}expression \end{gather*} $$ **文档P6.6** Description(描述):

A constant expression can be evaluated during translation rather than runtime, and accordingly may be used in any place that a constant may be.(常量表达式在编译阶段求值)

Constrains(约束条件):
  • Constant expressions shall not contain assignment, increment, decrement, function-call, or comma operators, except when they are contained within a subexpression that is not evaluated.(我的理解:常量表达式不应该包含赋值,递增,递减,函数调用,逗号运算符。除非是包含在子表达式里:如 sizeof)
  • Each constant expression shall evaluate to a constant that is in the range of representable values for its type.(每个常数表达式的计算值应在可表示的范围内其类型的值。)

Declarations(声明)

语法:

Constrains(约束条件):
  • A declaration shall declare at least a declarator (other than the parameters of a function or the members of a structure or union), a tag, or the members of an enumeration.

  • If an identifier has no linkage, there shall be no more than one declaration of the identifier (in a declarator or type specifier) with the same scope and in the same name space, except for tags.(如果标识符没有链接,则标识符(在声明符或类型说明符中)的声明不得超过一个,且范围和名称空间相同,标记除外。)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    int fun()
    {
    // 在函数里声明的变量是无链接的
    int a = 0;
    int a;
    }
    编译报错:
    main.c:63:9: error: redeclaration of 'a' with no linkage
    63 | int a;
    | ^
    main.c:62:9: note: previous definition of 'a' was here
    62 | int a=0;
  • All declarations in the same scope that refer to the same object or function shall specify compatible types.

Semantics(语法):
  • A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that:

    • for an object, causes storage to be reserved for that object;
    • for a function, includes the function body;
    • for an enumeration constant or typedef name, is the (only) declaration of the identifier.
  • The declaration specifiers consist of a sequence of specifiers that indicate the linkage, storage duration, and part of the type of the entities that the declarators denote. The init-declarator-list is a comma-separated sequence of declarators, each of which may have additional type information, or an initializer, or both. The declarators contain the identifiers (if any) being declared.

  • If an identifier for an object is declared with no linkage, the type for the object shall be complete by the end of its declarator, or by the end of its init-declarator if it has an initializer; in the case of function arguments (including in prototypes), it is the adjusted type (see 6.7.5.3) that is required to be complete

Storage-class specifiers

语法:

Constrains(约束条件):

At most, one storage-class specifier may be given in the declaration specifiers in a declaration.(一个声明中的声明说明符最多只能提供一个存储类说明符)

1
2
3
4
5
6
7
8
int fun()
{
static static int a = 0 ;
}
编译报错:
main.c:62:5: error: duplicate 'static'
62 | static static int a = 0 ;
| ^~~~~~

Type specifiers(类型说明符)

语法:

Structure and union specifiers(结构体和联合体说明符)

语法:

文档:6.7.2.1

Constrains(约束条件):
  1. A structure or union shall not contain a member with incomplete or function type (hence, a structure shall not contain an instance of itself, but may contain a pointer to an instance of itself), except that the last member of a structure with more than one named member may have incomplete array type; such a structure (and any union containing, possibly recursively, a member that is such a structure) shall not be a member of a structure or an element of an array.

  2. The expression that specifies the width of a bit-field shall be an integer constant expression that has nonnegative value that shall not exceed the number of bits in an object of the type that is specified if the colon and expression are omitted. If the value is zero, the declaration shall have no declarator.

  3. A bit-field shall have a type that is a qualified or unqualified version of _Bool, signed int, unsigned int, or some other implementation-defined type.

Enumeration specifiers(枚举说明符)

语法:

文档:P6.7.2.2

Constraints(约束条件):
The expression that defines the value of an enumeration constant shall be an integer
constant expression that has a value representable as an int.

Tags

文档P6.7.2.3

Type qualifiers

语法:

Constraints(约束条件):
Types other than pointer types derived from object or incomplete types shall not be restrict-qualified.

Function specifiers

语法:

Declarators

语法: