说明
本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/C1-C_intro/index.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。
1. By the C, by the C, by the Beautiful C
译注 · 标题的双关
章名套用了 1914 年的流行歌曲《By the Beautiful Sea》(在美丽的海边),把 sea(海)换成了读音相同的 C。这和书名「潜入系统」的海洋比喻呼应。
《By the Beautiful Sea》(在美丽的海边),Carroll 与 Atteridge,1914 年
“By the Beautiful Sea”, Carroll and Atteridge, 1914
本章面向已经用其他语言编过程序的学生,概述 C 语言编程。它是专门为 Python 程序员写的,用了一些 Python 示例作对比(附录 1是面向 Java 程序员的第 1 章版本)。不过,对于任何一门语言有基本编程经验的人来说,本章都可以作为 C 语言编程的入门介绍。
This chapter presents an overview of C programming written for students who have some experience programming in another language. It’s specifically written for Python programmers and uses a few Python examples for comparison purposes (Appendix 1 is a version of Chapter 1 for Java programmers). However, it should be useful as an introduction to C programming for anyone with basic programming experience in any language.
C 和你可能了解的其他语言(如 Python、Java、Ruby 或 C++)一样,是一种高级编程语言。它是一种命令式(imperative)、过程式(procedural)的编程语言,也就是说,C 程序表现为一系列供计算机执行的语句(步骤),并且 C 程序由一组函数(过程)构成。每个 C 程序都至少有一个函数,即 main 函数,其中包含程序启动时执行的语句。
C is a high-level programming language like other languages you might know, such as Python, Java, Ruby, or C++. It’s an imperative and a procedural programming language, which means that a C program is expressed as a sequence of statements (steps) for the computer to execute and that C programs are structured as a set of functions (procedures). Every C program must have at least one function, the
mainfunction, which contains the set of statements that execute when the program begins.
现代更新(截至 2026-11)· C 语言标准的新版本
本书写作时的最新 C 标准是 C17(ISO/IEC 9899:2018)。之后 ISO 于 2024 年 10 月发布了 ISO/IEC 9899:2024,即通常所说的 C23。它带来了 nullptr、把 true/false 变成关键字、十进制浮点类型等新特性。GCC 15 把默认的 C 语言模式从 -std=gnu17 改成了 -std=gnu23,依赖旧标准行为的代码需要显式加 -std= 选项。ISO/IEC 9899:2024 · cppreference:C23 · GCC 15 变更说明
与你可能熟悉的某些其他语言相比,C 语言对计算机机器语言的抽象程度更低。这意味着 C 不支持面向对象编程(不像 Python、Java 和 C++),也没有丰富的高级编程抽象(比如 Python 中的字符串、列表和字典)。因此,如果你想在 C 程序中使用字典这种数据结构,就得自己实现,而不能像 Python 那样直接导入语言自带的实现。
The C programming language is less abstracted from the computer’s machine language than some other languages with which you might be familiar. This means that C doesn’t have support for object-oriented programming (like Python, Java, and C++) or have a rich set of high-level programming abstractions (such as strings, lists, and dictionaries in Python). As a result, if you want to use a dictionary data structure in your C program, you need to implement it yourself, as opposed to just importing the one that is part of the programming language (as in Python).
C 缺少高级抽象,这可能让它看起来不那么吸引人。然而,正因为与底层机器之间的抽象较少,程序员更容易看清并理解程序代码与计算机执行之间的关系。C 程序员对程序在硬件上如何执行保有更多的控制权,能写出比用其他语言的高级抽象写成的等价代码运行得更高效的代码。尤其是,他们对程序如何管理内存有更多的控制,而这会对性能产生重大影响。因此,在底层控制和效率至关重要的计算机系统编程领域,C 仍然是事实上的标准语言。
C’s lack of high-level abstractions might make it seem like a less appealing programming language to use. However, being less abstracted from the underlying machine makes C easier for a programmer to see and understand the relationship between a program’s code and the computer’s execution of it. C programmers retain more control over how their programs execute on the hardware, and they can write code that runs more efficiently than equivalent code written using the higher-level abstractions provided by other programming languages. In particular, they have more control over how their programs manage memory, which can have a significant impact on performance. Thus, C remains the de facto language for computer systems programming where low-level control and efficiency are crucial.
本书使用 C,是因为它能清楚地表达程序的控制流程,而且翻译成计算机所执行的汇编和机器码也相对直接。本章介绍 C 语言编程,首先概述它的特性。第 2 章会更详细地讲解 C 的各项特性。
We use C in this book because of its expressiveness of program control and its relatively straightforward translation to assembly and machine code that a computer executes. This chapter introduces programming in C, beginning with an overview of its features. Chapter 2 then describes C’s features in more detail.