Dependable C
Dependable C (可靠的 C 语言)
Dependable C, is an attempt to document a subset of C for developers who want to write Dependable C. Imagine you are using a high level language, Like Python, Java or C#. You need a utility library to handle, some complicated maths, file parsing, or other basic functionality. What language would you want this library to be written in? The obvious one would be the language you are already using, but most languages aren’t stable enough for this. A 10 year old Python library is unlikely to run in the version of Python you run without constantly being maintained. You want it to be fast so a compiled language would be better. C++ and Rust suffer from the same backwards compatibility issues and need special toolchains, they also require special bindings to be used form other languages.
“Dependable C”旨在为那些希望编写“可靠 C 代码”的开发者记录 C 语言的一个子集。想象一下,你正在使用 Python、Java 或 C# 等高级语言,你需要一个工具库来处理复杂的数学运算、文件解析或其他基础功能。你希望这个库是用什么语言编写的呢?最显而易见的答案是你正在使用的语言,但大多数语言并不具备足够的稳定性。一个 10 年前的 Python 库,如果不经过持续维护,几乎不可能在你当前的 Python 版本中运行。你希望它运行速度快,所以编译型语言会是更好的选择。然而,C++ 和 Rust 同样存在向后兼容性问题,需要特殊的工具链,并且需要专门的绑定才能被其他语言调用。
What you want is a library written in C. Its fast, simple, runs everywhere and in a pinch most people can read the code. But what kind of C do you want it to be written in? You want it to be written in the plainest possible C, that doesn’t require any extensions, compiler settings, build steps. You should be able to use it with whatever toolchain your platform has available and not rely on a specific implementation or version of the language. This is a library written in Dependable C. Its not fancy, its not “modern” but it works, always and it runs fast.
你真正需要的是一个用 C 语言编写的库。它快速、简洁、随处可运行,而且在紧急情况下,大多数人都能读懂这些代码。但你希望它是用什么样的 C 语言编写的呢?你希望它用最朴素的 C 语言编写,不需要任何扩展、编译器设置或复杂的构建步骤。你应该能够使用平台提供的任何工具链来使用它,而不必依赖于特定的实现或语言版本。这就是用“Dependable C”编写的库。它不花哨,也不“现代”,但它总是能正常工作,且运行速度极快。
The most important feature of any code is that you can make it compile and run as intended. That’s what Dependable C prioritizes. Not the ergonomics of the programmer, but how much the user can depend on it working for their use. Very few people want to write Dependable C, but everyone wishes everyone else wrote their code in Dependable C.
任何代码最重要的特性是能够按预期编译并运行。这正是 Dependable C 所优先考虑的。它关注的不是程序员的开发舒适度,而是用户在实际使用中对代码可靠性的依赖程度。很少有人愿意主动去写 Dependable C,但每个人都希望别人能用 Dependable C 来编写代码。
C23, and the upcoming C2Y are language versions that have become increasingly complex, include many new keywords, flow control, and a revised Charter that differs from “Classic C”. Later versions of C are also only supported by two implementations out of the hundreds of C implementations available. The Delta between ANSI C and C2Y is arguably larger than the Delta between ANSI C and the first version of C++. This means that for developers who want to develop, widely portable, and compliant, software in Classic C, the latest ISO C standards are a poor guide. Reading earlier versions of standards is also not sufficient, since they do not include lists of features that have since been deprecated, or any guide as to what parts of the standard have had poor implementation support. This is why Dependable C exists, its a guide to help you write C code that is universally accepted.
C23 以及即将推出的 C2Y 等语言版本变得越来越复杂,包含了许多新的关键字、流程控制,以及与“经典 C”不同的修订章程。在现有的数百种 C 语言实现中,只有两种支持这些较新的版本。ANSI C 与 C2Y 之间的差异,可以说比 ANSI C 与第一版 C++ 之间的差异还要大。这意味着,对于那些希望在“经典 C”中开发具有广泛可移植性和合规性软件的开发者来说,最新的 ISO C 标准并不是好的指南。阅读早期的标准版本也不够充分,因为它们没有包含已被弃用的功能列表,也没有关于标准中哪些部分实现支持较差的指导。这就是 Dependable C 存在的原因,它是一份旨在帮助你编写被普遍接受的 C 代码的指南。
The Universal storage of computing instructions. C is the most portable and widely implemented language available. C has been called the lingua franca of computing. A problem solved in C will remain solved for the foreseeable future. Changes in operating systems, computing environments, or hardware are unlikely to render a well written C implementation obsolete. A library written in C will be able to be used from almost any language. While many programmers don’t use C, many can read and understand C. This mean that code written in C can be modified by a larger pool of programmers. If quality is the measure of longevity, C is a prime candidate for writing high quality code.
计算指令的通用存储。C 语言是目前可移植性最强、实现最广泛的语言。C 语言被称为计算领域的“通用语”(lingua franca)。用 C 语言解决的问题,在可预见的未来依然有效。操作系统、计算环境或硬件的变化,不太可能使编写良好的 C 实现过时。用 C 语言编写的库几乎可以被任何语言调用。虽然许多程序员不使用 C 语言,但很多人都能阅读和理解它。这意味着用 C 语言编写的代码可以由更多的程序员进行修改。如果以质量作为衡量长寿的标准,C 语言是编写高质量代码的首选。
Not all C code is portable, or will compile the same in all compilers, or can even be understood by most C programmers. C has a long history of quirks, and corner cases that can be hard to navigate. Writing non-portable code that is only intended to run on one platform and be built with a particular tool chain is perfectly legitimate, but if you want to write code that is portable, and remains usable for decades this guide if for you. Who values writing code that is guaranteed to compile and work correctly, over having the latest language features.
并非所有的 C 代码都具有可移植性,也不是在所有编译器中都能以相同方式编译,甚至并非都能被大多数 C 程序员所理解。C 语言有着悠久的怪癖历史和难以驾驭的边缘情况。编写仅打算在单一平台运行并使用特定工具链构建的非可移植代码是完全合理的,但如果你想编写可移植且能使用数十年的代码,这份指南就是为你准备的。它适合那些比起追求最新语言特性,更看重代码能否保证编译并正确运行的开发者。
Dependable C is the opposite of a dialect. It is a C that is trying to be as middle of the road as possible in order to be understood and implemented as widely as possible. Think of it as Newscaster C, a neutral, universally understood, language. Dependable C is not a style guide, it does not prescribe formatting, indentation and style. It simply tries to document what C functionality can be depended on and how. It is perfectly valid to use Dependable C as a guide for what functionality to use, while at the same time to adhere to a style guide like Misra. The Misra standard prioritizes safety, where as Dependable C prioritizes Compatibility. It is entirely possible to adhere both at the same time.
Dependable C 与“方言”恰恰相反。它是一种力求走“中间路线”的 C 语言,旨在尽可能被广泛理解和实现。你可以把它想象成“新闻播音员式的 C 语言”(Newscaster C),一种中立的、通用的语言。Dependable C 不是风格指南,它不规定格式、缩进和风格。它只是试图记录哪些 C 语言功能是可靠的以及如何使用它们。将 Dependable C 作为使用功能的指南,同时遵守 Misra 等风格指南是完全可行的。Misra 标准优先考虑安全性,而 Dependable C 优先考虑兼容性。两者完全可以同时兼顾。