Skip to content

Before the compiler

C Programming Lesson 23 2:31 English narration · English + 中文 subtitles burned in

space play · ←/→ 5s · j/l 10s · f fullscreen · ,/. speed

Chapters

Transcript
Something runs before the compiler ever sees your file. 在编译器看到你的文件之前,有个东西会先跑一遍。
It is called the preprocessor, and it handles every line beginning with a hash. 它叫预处理器,负责处理每一行以 # 开头的内容。
You have used it since lesson one: hash include pastes in the contents of a header. 从第一课起你就在用它了:#include 会把一个头文件的内容贴进来。
Hash define does the other job — it swaps in the text. #define 做的是另一件事——它替换文本。
Write hash define PI and every PI in your file becomes that number before compiling. 写下 #define PI,文件里每一个 PI 在编译之前就会被换成那个数字。
Which is why a macro has no type at all: by the time the compiler looks, PI is gone. 这也是为什么宏根本没有类型: 等编译器来看的时候,PI 已经不存在了。
So there are two ways to name a constant, and they are not the same kind of thing. 所以给一个常量起名有两种方式,而它们根本不是同一类东西。
A macro is text with no type. 宏是一段没有类型的文本。
A const is a real variable that simply cannot be changed — it has a type, so the compiler checks how you use it and tells you when it does not fit. const 是一个真正的变量,只不过它不能被改变—— 它有类型,所以编译器会检查你怎么使用它, 用得不对时会告诉你。
Modern C prefers const for values, while hash define stays normal for flags and for switching whole blocks of code on and off. 现代 C 在表示数值时更倾向用 const, 而 #define 在表示开关标志、 以及整块代码的启用和关闭时,依然很常见。
Now something that contradicts what you learned about locals. 现在来看一个和你学过的局部变量相矛盾的东西。
A local variable is normally created fresh every time the function runs. 局部变量通常在函数每次运行时都重新创建一次。
Mark it static and it is set up once, and it keeps its value between calls. 给它加上 static,它就只被初始化一次, 并且在两次调用之间保留自己的值。
That is exactly what the third task needs: a counter that gives out one, then two, then three. 这正是第三道题需要的: 一个依次发出 1、2、3 的计数器。
Without static, it would be reset to nought on every call, and the answer would be one, forever. 没有 static,它每次调用都会被重置为 0, 答案就永远是 1。
Last piece: enum, which gives names to a set of related whole numbers. 最后一块:enum,它给一组相关的整数起名字。
Declare the four suits and C numbers them for you, counting from zero — so HEARTS really is nought, and SPADES is three. 声明四种花色,C 会替你编号,从 0 开始数—— 所以 HEARTS 真的就是 0,SPADES 是 3。
An enum is int underneath with readable names on top, and that is the entire benefit: code that says HEARTS instead of nought is code you can still read next month. 枚举底层就是 int,只是上面套了一层可读的名字, 而这就是它的全部好处: 写 HEARTS 而不是 0 的代码,下个月你还读得懂。
Four things to take with you. 带走四点。
One: the preprocessor rewrites your text before compiling. 第一:预处理器在编译之前重写你的文本。
Two: hash define is replacement, while const is a checked value. 第二:#define 是替换,而 const 是一个会被检查的值。
Three: a static local keeps its value between calls. 第三:static 局部变量在两次调用之间保留它的值。
Four: an enum gives readable names to plain ints. 第四:enum 给普通整数起了可读的名字。
Do the three tasks, and that is the whole course — twenty-three lessons, from your first printf to lists, files and bits. 把那三道题做完,整门课程就结束了—— 二十三课,从你的第一个 printf,一直到链表、文件和二进制位。

Log in or create account

IGCSE, A-Level & AP