==============================================================
C 编程语言的当前标准是 ISO/IEC 9899:2024。
更多信息请阅读 ANSI 博客:当前 C 编程语言标准——ISO/IEC 9899:2024 (C24)
C 编程语言标准 (ISO/IEC 9899:2024) [当前] - ANSI 博客
什么是 C 语言?
如今,C 是全球最流行的编程语言之一,广泛用于操作系统、网络驱动程序、数据库、语言解释器、工具程序以及其他多种用途。虽然它起源于 20 世纪 70 年代,但其现有的实践在标准 ANSI X3.159-1989(即 C89)中得到了统一和扩展。由于这些起源,许多人仍然将 C 语言标准称为 ANSI C。然而,C 标准在 1990 年被国际采纳,并且长期以来一直是 ISO/IEC 9899,该标准由 ISO/IEC JTC 1 开发。C 语言实践整合到国际标准中促使其与 ISO C 名称的关联。
什么是 ISO/IEC 9899?
ISO/IEC 9899:2024 规定了用 C 编程语言编写的程序的形式,并建立了对其解释的标准。根据其范围,它规定了以下内容:
- C 在程序中的表示形式
- C 语言的语法和约束
- 解释 C 程序的语义规则
- 供 C 程序处理的输入数据的表示形式
- 由 C 程序生成的输出数据的表示形式
- 符合标准的实现所施加的限制和限制条件
请注意,ISO/IEC 9899:2024 并未指定:
- 将 C 程序转换为可供数据处理系统使用的机制,
- 调用 C 程序以供数据处理系统使用的方法,
- 输入数据转换为可供 C 程序使用的方式,
- 或输出数据在由 C 程序生成后的转换方式。
该标准也不涉及程序及其数据的大小或复杂性超出特定数据处理系统容量的问题,也没有涵盖支持符合标准的实现所需的数据处理系统的全部最低要求。
ISO/IEC 9899:2024 的结构
ISO/IEC 9899:2024 文档分为四个主要部分:
- 初步元素(见第 1-4 条)
- 翻译和执行 C 程序的环境特性(第 5 条)
- 语言的语法、约束和语义(第 6 条)
- 库设施(第 7 条)
用户应注意,由于 ISO/IEC 9899 会定期修订,新功能可能会被添加或从文档中移除。因此,某些用法可能会与未来版本冲突。
ISO/IEC 9899:2024 的变更
ISO/IEC 9899:2024(即 C24)是对 ISO/IEC 9899:2021(即 C18)的修订。此国际标准进行了大量修改,包括但不限于:
- 添加了新的关键字,例如
bool
、static_assert
、true
、false
、thread_local
等,并允许实现提供旧拼写形式的宏(以下划线开头后跟大写字母),同时定义旧关键字和新关键字作为宏以方便程序过渡。 - 移除了整数宽度约束和过时的符号表示(所谓的“1 的补码”和“符号-幅值”)。
- 添加了单参数版本的
static_assert
。 - 删除了对带有标识符列表的函数定义的支持。
- 对于空参数列表的函数声明,现在与仅包含一个
void
的参数列表等同对待。 - 通过扩展
strftime
的月份名称格式以及集成函数(如gmtime_r
、localtime_r
、memccpy
、strdup
、strndup
),使标准与 ISO/IEC 9945(POSIX)保持一致。 - 与浮点标准 ISO/IEC 60559 保持一致,包括集成二进制浮点技术规范 ISO/IEC TS 18661-1:2014、十进制浮点技术规范 ISO/IEC TS 18661-2:2015、浮点类型技术规范 ISO/IEC TS 18661-3:2015、数学函数技术规范 ISO/IEC TS 18661-4:2015,以及为 ISO/IEC 60559:2020 推荐的操作添加新的最大值和最小值函数。
- 使
DECIMAL_DIG
宏过时。 - 向库头文件添加了版本测试宏,以便与
__STDC_VERSION__
宏一起使用,帮助升级和移植。 - 允许在声明前或复合语句末尾放置标签。
- 添加了属性功能,包括以下属性:
deprecated
:标记未来不推荐使用的实体。fallthrough
:显式标记 switch 或标签中贯穿的情况,而非意外。maybe_unused
:标记可能未被使用的实体。nodiscard
:标记当使用时,其值应在程序中以某种方式处理的实体。noreturn
:指示函数永远不会返回。reproducible
:标记函数类型,对于相同的输入始终产生可预测的输出(例如缓存数据),但调用顺序仍很重要。unsequenced
:标记函数类型,始终产生可预测的输出且不依赖其他数据(及其他相关注意事项)。
- 添加了
u8
字符前缀以匹配u8
字符串前缀。 - 强制所有
u8
、u
和U
字符串分别符合 ISO/IEC 10646 中定义的 UTF-8、UTF-16 和 UTF-32 标准。 - 分离了字符串和字符的文字、宽文字、UTF-8 文字、UTF-16 文字和 UTF-32 文字编码,并引入了纯粹基于执行版本的实现,特别是执行编码和宽执行编码。
- 添加了缺失的
mbrtoc8
和c8rtomb
函数。 - 复合字面量现在可以包含存储类说明符作为类型的一部分,以更改复合字面量的生命周期(并可能将其转换为常量表达式)。
- 添加了对象定义的
constexpr
说明符,并改进了与constexpr
存储类说明符结合使用的常量表达式的识别。 - 增加了对用空大括号初始化对象的支持。
- 添加了
typeof
和typeof_unqual
操作以推导表达式的类型。 - 改进了标记兼容性规则,使更多类型能够与其他类型兼容。
- 添加了位精确整数类型
_BitInt(N)
和unsigned _BitInt(N)
,其中N
可以是从 1 到BITINT_MAXWIDTH
的整数常量表达式。 - 改进了对没有基础类型的枚举的处理规则,特别是允许没有固定基础类型的枚举具有比
int
更广范围的值表示。 - 添加了新的冒号分隔类型说明符,用于指定枚举的固定基础类型(并且,根据实现定义的规则,此类构造遵循固定基础类型的填充、对齐和大小规则,适用于结构体、联合体及位域)。
- 添加了一个新头文件和一组位和字节操作工具,以方便访问许多实现中最高效的特性。
- 修改了现有函数以保留传递给函数的类型的
const
属性。 - 添加了一项功能,通过新的预处理器指令
#embed
尽可能忠实地嵌入二进制数据。 - 添加了
nullptr
常量和具有明确底层表示的nullptr_t
类型,其表示与void*
指针相同。 - 添加了
VA_OPT
说明符,并澄清了宏调用和参数处理的语言。 - 强制支持可变修改类型(但不包括可变长度数组本身)。
此列表仅包含对标准进行的大量更改中的一半左右。用户可以在文档的附录 M 中找到 ISO/IEC 9899 的变更历史。
获取标准
ISO/IEC 9899:2024 – 信息技术 – 编程语言 – C 可在 ANSI Webstore 上获取。
更多信息请阅读 ANSI 博客:当前 C 编程语言标准——ISO/IEC 9899:2024 (C24)
C 编程语言标准 (ISO/IEC 9899:2024) [当前] - ANSI 博客
=======================================================
The current standard for the programming language C is ISO/IEC 9899:2024 – Information technology – Programming languages – C.
What Is C?
Today, C is one of the most popular programming languages used worldwide for operating systems, network drivers, databases, language interpreters, and utilities, as well as a variety of other purposes. While it originated in the 70s, the existing practices of C were unified and expanded upon in the standard ANSI X3.159-1989, or C89. Because of these origins, many people still call the C programming language standard ANSI C. However, the C standard was adopted internationally in 1990, and it has long been ISO/IEC 9899, which is developed by ISO/IEC JTC 1. The integration of C practices into an international standard sparked its association with the name ISO C.
What Is ISO/IEC 9899?
ISO/IEC 9899:2024 specifies the form and establishes the interpretation of programs written in the C programming language. As stated in its scope, it specifies:
- The representation of C in programs
- The syntax and constraints of the C language
- The semantic rules for interpreting C programs
- The representation of input data to be processed by C programs
- The representation of output data processed by C programs
- The restrictions and limits imposed by a conforming implementation of C
Please note, however, that ISO/IEC 9899:2024 does not specify:
- The mechanisms by which C programs are transformed for use by a data-processing system,
- C programs are invoked for use by a data-processing system,
- Input data are transformed for use by a C program, or
- Output data are transformed after being produced by a C program.
The standard also doesn’t address the size or complexity of a program and its data that will exceed the capacity of any specific data-processing system, nor does it cover all minimal requirements of a data-processing system that is capable of supporting a conforming implementation.
ISO/IEC 9899:2024 Structure
The ISO/IEC 9899:2024 document is divided into four major subdivisions:
- Preliminary elements (found in Clauses 1-4)
- The characteristics of environments that translate and execute C programs (Clause 5)
- The language syntax, constraints, and semantics (Clause 6)
- The library facilities (Clause 7)
Users should note that, since ISO/IEC 9899 undergoes periodic revision, new features could be added to or withdrawn from the document. Therefore, certain uses could conflict with future editions.
Changes to ISO/IEC 9899:2024
ISO/IEC 9899:2024, or C24, is a revision of ISO/IEC 9899:2021, or C18. Numerous changes were made to this international standard, including:
- New keywords were added, such as
bool
,static_assert
,true
,false
,thread_local
, and others, and implementations were allowed to provide macros for the older spelling with a leading underscore followed by a capital letter as well as defining old and new keywords as macros to enable transition of programs easily. - Integer width constraints and obsolete sign representations (so-called “1’s complement” and “sign-magnitude”) were removed.
- One-argument version of
static_assert
was added. - Support was removed for function definitions with identifier lists.
- Mandated function declarations whose parameter list is empty are now treated the same as a parameter list which only contain a single
void
. - The standard was harmonized with ISO/IEC 9945 (POSIX) through extended month name formats for
strftime
and integration of functions:gmtime_r
,localtime_r
,memccpy
,strdup
,strndup
. - The standard was harmonized with floating-point standard ISO/IEC 60559, including integration of binary floating-point technical specification ISO/IEC TS 18661-1:2014, decimal floating-point technical specification ISO/IEC TS 18661-2:2015, floating-point types technical specification ISO/IEC TS 18661-3:2015, mathematical functions technical specification ISO/IEC TS 18661-4:2015, and new maximum and minimum functions for operations recommended in ISO/IEC 60559:2020.
- The
DECIMAL_DIG
macro was made obsolescent. - Version test macros were added to library headers that contained changes to aid in upgrading and portability to be used alongside the
__STDC_VERSION__
macro. - Placement of labels was allowed in front of declarations and at the end of compound statement.
- The attributes feature was added, which includes the attributes:
deprecated
, for marking entities as discouraged for future usefallthrough
, for explicitly marking cases where falling through in switches or labels is intended rather than accidentalmaybe_unused
, for marking entities which can end up not being usednodiscard
, for marking entities which, when used, should have their value handled in some way by a programnoreturn
, for indicating a function shall never returnreproducible
, for marking function types for which inputs always produce predictable output if given the same input (e.g., cached data) but for which the order of such calls still mattersunsequenced
, for marking function types which always produce predictable output and have no dependencies upon other data (and other relevant caveats).
- The
u8
character prefix was added to match theu8
string prefix. - All
u8
,u
, andU
strings were mandated to be UTF-8, UTF-16, and UTF-32, respectively, as defined by ISO/IEC 10646. - Separated the literal, wide literal, and UTF-8 literal, UTF-16 literal, and UTF-32 literal encodings for strings and characters and now have a solely execution-based version of these, particularly execution and wide execution encodings.
mbrtoc8
andc8rtomb
functions missing were added.- Compound literals now may also include storage-class specifiers as part of the type to change the lifetime of the compound literal (and possibly turn it into a constant expression).
- The
constexpr
specifier for object definitions was added, and the standard improved what is recognized as a constant expression in conjunction with theconstexpr
storage-class specifier. - Support was added for initialization of objects with empty braces.
- The
typeof
andtypeof_unqual
operations were added for deducing the type of an expression. - Tag compatibility rules were improved, enabling more types to be compatible with other types.
- Bit-precise integer types
_BitInt(N)
andunsigned _BitInt(N)
were added, whereN
can be an integer constant expression whose value is from one toBITINT_MAXWIDTH
, inclusive. - Rules were improved for handling enumerations without underlying types, in particular allowing for enumerations without fixed underlying type to have value representations that have a greater range than
int
. - A new colon-delimited type specifier was added for enumerations to specify a fixed underlying type (and which, subject to an implementation’s definitions governing such constructs, adopt the fixed underlying type’s rules for padding, alignment, and sizing within structures and unions as well as with bit-fields).
- A new header and a suite of bit and byte-handling utilities were added for portable access to many implementations’ most efficient functionality.
- Existing functions were modified to preserve the
const
-ness of the type placed into the function. - A feature was added to embed binary data as faithfully as possible with a new preprocessor directive
#embed
. - A
nullptr
constant and anullptr_t
type with a well-defined underlying representation identical to a pointer tovoid
were added. - The
VA_OPT
specifier was added and language was clarified in the handling of macro invocation and arguments. - Support was mandated for variably modified types (but not variable length arrays themselves).
This list only includes roughly half of the extensive changes made to this standard. Users can find the change history of ISO/IEC 9899 in Annex M of the document.
Availability
ISO/IEC 9899:2024 – Information technology – Programming languages – C is available on the ANSI Webstore.
Read more at the ANSI Blog: The Current C Programming Language Standard – ISO/IEC 9899:2024 (C24).