Skip to content

The C skeleton

C Programming Lesson 1 2:29 English narration · English + 中文 subtitles burned in

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

Chapters

Transcript
C works differently from the languages elsewhere on this site, and the difference shapes everything else. C 的工作方式和这个网站上其他语言不同,而这个差别决定了其余的一切。
You write the source file. 你先写源文件。
Then a compiler reads the whole thing and turns it into a program that runs directly on the machine — which is why C is fast, and why it is still under your operating system and your phone. 然后编译器把整个文件读一遍, 把它变成一个直接在机器上运行的程序—— 这就是 C 快的原因,也是为什么你的操作系统和手机底层至今还是它。
The price is that mistakes are caught by the compiler, before a single line has run, and it will not start until you fix them. 代价是:错误由编译器发现,在任何一行运行之前, 而在你改好之前,它根本不会开始运行。
Every C program you will write starts with the same shape, so learn it once. 你写的每一个 C 程序都从同样的形状开始,所以把它学会一次就够了。
The include line at the top borrows printf from the standard library — without it the compiler does not know that word. 顶部的 include 那一行,把 printf 从标准库里借过来—— 没有它,编译器根本不认识这个词。
Then a function called main, which is where the program begins; the operating system calls it for you. 然后是一个叫 main 的函数,程序从这里开始;操作系统会替你调用它。
And at the end, return zero, which tells the system the program finished without trouble. 最后是 return 0,它告诉系统程序顺利结束了。
printf takes a sentence with holes in it, and then the values to drop in. printf 接收一个带空位的句子,后面再跟上要填进去的值。
Each hole names the KIND of value it expects. 每个空位都说明它期待的是哪一种值。
Percent d for a whole number, percent f for a decimal, percent c for a single character — get the letter wrong and you get nonsense, because C simply believes you. %d 表示整数,%f 表示小数,%c 表示单个字符—— 字母写错就会得到乱七八糟的结果,因为 C 就是照你说的信。
And printf ends no line by itself: if you want the next output on a new line, you write backslash n yourself. 而且 printf 自己不会换行:想让下一次输出另起一行,就得自己写 \n。
Now the lesson's second task: a function that squares a number. 现在做课程里的第二道题:一个求平方的函数。
Look at how much C asks you to state. 注意 C 要求你说明多少东西。
The word int at the front is what it hands back. 前面的 int 是它交还回来的类型。
The int inside the brackets is what it takes in. 括号里的 int 是它接收的类型。
Neither can be left out, and neither can be wrong. 两个都不能省,两个都不能写错。
That is not bureaucracy — it is exactly what lets the compiler check your program from top to bottom before it runs. 这不是繁文缛节——正是这些东西, 让编译器能在程序运行之前把它从头到尾检查一遍。
Four things to take with you. 带走四点。
One: C is compiled first, so errors arrive before the program runs. 第一:C 先编译,所以错误在程序运行之前就出现了。
Two: it always begins at main, and ends with return zero. 第二:它总是从 main 开始,以 return 0 结束。
Three: printf's holes name a type — percent d, percent f, percent c. 第三:printf 的空位要说明类型——%d、%f、%c。
Four: printf adds no newline, so write backslash n where you want one. 第四:printf 不会自动换行,需要换行就自己写 \n。
Now do the three tasks; the first one is Hello, C, on its own line. 现在去做那三道题;第一题是让 Hello, C! 单独占一行。

Log in or create account

IGCSE, A-Level & AP