说明
本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/Appendix1/index.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。
16. Appendix 1: Chapter 1 for Java Programmers
C 啊,C 啊,美丽的 C——《在美丽的海边》(By the Beautiful Sea),Carroll 与 Atteridge,1914 年
By the C, by the C, by the Beautiful C –”By the Beautiful Sea”, Carroll and Atteridge, 1914
原书注
本附录是为 Java 程序员编写的第 1 章版本。除了用 Java 示例代替第 1 章中的 Python 示例进行对比之外,它的内容与第 1 章几乎完全相同。
This appendix is a version of Chapter 1 written for Java programmers. Its content is almost identical to Chapter 1 except that it uses Java examples for comparison purposes instead of the Python examples used in Chapter 1.
本附录为有其他语言编程经验的学生概述 C 语言编程。它专门为 Java 程序员编写,并用一些 Java 示例进行对比。不过,对于任何有某种语言基本编程经验的人来说,它都应该能作为 C 语言编程的入门。
This appendix presents an overview of C programming written for students who have some experience programming in another language. It’s specifically written for Java programmers and uses a few Java examples for comparison purposes. 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 不支持面向对象编程,没有丰富的高级编程抽象(例如 String 和 ArrayList)和供程序员使用的大量类库,也不支持垃圾回收和异常。因此,如果你想在 C 程序中使用字典这样的数据结构,就需要自己实现它,而不能像在 Java 中那样直接使用类库中已经实现好的。
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, nor does it have a rich set of high-level programming abstractions (like String and ArrayList) and large set of class libraries for programmers to use, nor does it have support for garbage collection and exceptions. As a result, if you want to use a data structure like a dictionary in your C program, you need to implement it yourself, as opposed to just using one already implemented in a Java class library.
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,是因为它对程序控制的表达能力,以及它相对直接地对应到计算机所执行的汇编代码和机器码。本章面向熟悉 Java 的读者介绍 C 语言编程,首先概述 C 的特性以及它们与 Java 编程语言的关系。第 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 to readers familiar with Java, beginning with an overview of C’s features and how they related to the Java programming language. Chapter 2 then describes C’s features in more detail.