非常教程

C参考手册

C 语法

Identifier

一个标识符是任意长的数字序列,下划线,小写和大写拉丁字母,并且使用 Unicode 字符指定\u\U逃逸符号(自 C99)。有效的标识符必须以非数字字符(拉丁字母,下划线或 Unicode 非数字字符(自C99以来)开始)。标识符区分大小写(小写和大写字母不同)。

如果在标识符中允许原始(未转义的)Unicode字符,则它是实现定义的:char * \ U0001f431 =“cat”; //支持char *?=“猫”; //实现定义(例如,与Clang一起工作,但不是GCC)

(自C99以来)

标识符可以表示以下类型的实体:

  • 对象
  • 功能
  • 标记(结构,联合或枚举)
  • 结构或联盟成员
  • 枚举常量
  • typedef名称
  • 标签名称
  • 宏名称
  • 宏参数名称

除宏名称或宏参数名以外的每个标识符都具有范围,属于名称空间,并且可能具有链接。如果实体位于不同的名称空间中,相同的标识符可以表示程序中不同点处的不同实体,或者可以表示同一点处的不同实体。

保留标识符

以下标识符是保留的,不能在程序中声明(这样做会调用未定义的行为):

1)作为关键字的标识符不能用于其他目的。特别是 #define 或 #undef 与一个关键字相同的标识符是不允许的。

2)以下划线开头的所有外部标识符。

3)以下划线开头的所有标识符,后跟大写字母或其他下划线(这些保留的标识符允许库使用大量幕后的非外部宏和函数)

4)由标准库定义的所有外部标识符(在托管环境中)。这意味着不允许用户提供的外部名称与任何库名称匹配,即使声明与库函数相同的函数也是如此。

5)标识符被声明为保留以供将来由标准库使用,即

  • 函数名称
    • cerfcerfccexp2cexpm1clog10clog1pclog2clgammactgamma和他们-f -l和后缀的变体,在<complex.h>
    • 开头isto后面跟着一个小写字母,in <ctype.h>wctype.h
    • str以小写字母开头,后面跟着一个小写字母<stdlib.h>
    • 开头strmemwcs后跟一个小写字母,在<string.h>
    • wcs以小写字母开头,后面跟着一个小写字母<wchar.h>
    • atomic_以小写字母开头,后面跟着一个小写字母<stdatomic.h>
    • 开头cnd_mtx_thrd_tss_后跟一个小写字母,在<threads.h>
  • typedef名称
    • 开头intuint和结尾_t,在<stdint.h>
    • 以小写字母开头atomic_memory_后面跟着一个小写字母<stdatomic.h>
    • 开头cnd_mtx_thrd_tss_后跟一个小写字母,在<threads.h>
  • 宏名称
    • E以数字或大写字母开头,后面跟着一个数字或一个大写字母<errno.h>
    • FE_以大写字母开头,后面跟着一个大写字母<fenv.h>
    • 开头INTUINT与结尾_MAX_MIN_C<stdint.h>
    • 开头PRISCN后跟小写字母或字母X,在<stdint.h>
    • LC_以大写字母开头,后面跟着一个大写字母<locale.h>
    • 以大写字母开头SIGSIG_后面跟着一个大写字母<signal.h>
    • 开始与TIME_后跟大写字母,在<time.h>中
    • ATOMIC_以大写字母开头,后面跟着一个大写字母<stdatomic.h>
  • 枚举常量
    • memory_order_以小写字母开头,后面跟着一个小写字母<stdatomic.h>
    • 开头cnd_mtx_thrd_tss_后跟一个小写字母,在<threads.h>

所有其他标识符都可用,不必担心在将程序从一个编译器和库移动到另一个编译器和库时发生意外冲突。

注意:在C ++中,任何地方都带有双下划线的标识符都是保留的; 在C中,只保留以双下划线开头的那些。

翻译限制

即使对标识符的长度没有特别的限制,早期编译器对标识符中重要的初始字符的数量也有限制,连接器对外部链接的名称施加了更严格的限制。C要求任何符合标准的实现至少支持以下限制:

内部标识符或宏名称中的31个重要初始字符外部标识符中的6个重要初始字符511个一个翻译单元中的外部标识符127个在一个块中声明块范围的标识符在一个预处理翻译单元中同时定义的1024个宏标识符

(直到C99)

内部标识符或宏名称中的63个重要起始字符外部标识符中的31个重要起始字符4095一个翻译单元中的外部标识符511个在一个块中声明块范围的标识符4095在一个预处理翻译单元中同时定义的宏标识符

(自C99以来)

  • 内部标识符或宏名称中的31个重要的初始字符
  • 外部标识符中有6个重要的初始字符
  • 一个翻译单元中有511个外部标识符
  • 在一个块中声明块范围的127个标识符
  • 在一个预处理翻译单元中同时定义了1024个宏标识符

(until C99)

  • 内部标识符或宏名称中有63个重要的初始字符
  • 外部标识符中有31个重要的初始字符
  • 一个翻译单元中包含4095个外部标识符
  • 在一个块中声明块范围的511个标识符
  • 在一个预处理翻译单元中同时定义了4095个宏标识符

(since C99)

参考

  • C11 standard (ISO/IEC 9899:2011):
    • 5.2.4.1 Translation limits (p: 25-26)
    • 6.4.2 Identifiers (p: 59-60)
    • 6.10.8 Predefined macro names (p: 175-176)
    • 6.11.9 Predefined macro names (p: 179)
    • 7.31 Future library directions (p: 455-457)
    • K.3.1.2 Reserved identifiers (p: 584)
  • C99 standard (ISO/IEC 9899:1999):
    • 5.2.4.1 Translation limits (p: 20-21)
    • 6.4.2 Identifiers (p: 51-52)
    • 6.10.8 Predefined macro names (p: 160-161)
    • 6.11.9 Predefined macro names (p: 163)
    • 7.26 Future library directions (p: 401-402)
  • C89/C90 standard (ISO/IEC 9899:1990):
    • 2.2.4.1 Translation limits
    • 3.1.2 Identifiers
    • 3.8.8 Predefined macro names

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.if statement
52.Implicit conversions
53.Increment/decrement operators
54.Initialization
55.inline function specifier
56.integer constant
57.Lifetime
58.Logical operators
59.Lookup and name spaces
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 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。