The virtues of C.
- Efficiency(高效性)
- Portability(可移植性)
- Power and Flexibility(强大且灵活)
- Programmer Oriented(程序员导向的)
Shortcomings
freedom lead to fault
Using C:Seven Steps
c语言是编译型语言,我们可以把它拆分为7步来看
编程的七步:
1、定义程序的目标
2、设计程序
3、写代码
4、编译
5、运行程序
6、测试并调试程序
7、维护并修改程序
编译器和链接器
c语言运行过程:
将concrete.c文件(源代码)经过编译器编译成目标代码(concrete.obj),之后通过链接器连接资源code和启动code生成可执行的代码(concrete.exe)
Unix写c语言程序的过程:
输入源代码,通过Text Editor(编辑器)编辑code,生成源代码(name.c),之后进行编译,编译之后会生成a.out文件
如果想保留这个文件就要对它进行重命名,否则下一次编译name.c时产生的.out文件会覆盖原来的文件
What about the object code? The cc compiler creates an object code file having the same basename as the source code, but with an .o extension. In our example, the object code file is called inform.o, but you won’t find it, because the linker removes it once the executable program has been completed. However, if the original program used more than one source code file, the object code files would be saved. When we discuss multiple-file programs later in the text, you will see that this is a fine idea.
目标代码会在a.out编译完成后被删除,但是如果a.out文件编译时用到多个源文件,那么object code(inform.o)就会被保存下来
You can run the program by typing the basename at the command line:
在command line输入文件名就可以运行编译好的文件
D:\testFile\C>test
'test' 不是内部或外部命令,也不是可运行的程序或批处理文件。
D:\testFile\C>gcc test.c -o test.exe
D:\testFile\C>test
hello world
D:\testFile\C>
直接在command line中输入文件名并运行时是直接调用编译好的.exe文件,如果源代码没有编译,输入文件名运行的时候就会报错
使用command line对source code进行编译时,如果不声明executable file的名称,会默认生成为a.exe
如果要声明文件名称就使用以下代码
gcc demo.c -o demo.exe
One problem you might encounter is that the window showing the program execution vanishes when the program terminates. If that is the case for you, you can make the program pause until you press the Enter key. To do that, add the following line to the end of the program, just before the return statement:
getchar();
This line reads a keystroke, so the program will pause until you press the Enter key. Sometimes, depending on how the program functions, there might already be a keystroke waiting. In that case,you’ll have to use getchar() twice:
getchar(); getchar();
For example, if the last thing the program did was ask you to enter your weight, you would havetyped your weight and then pressed the Enter key to enter the data. The program would read the weight, the first getchar() would read the Enter key, and the second getchar() would cause the program to pause until you press Enter again. If this doesn’t make a lot of sense to you now, it will after you learn more about C input. And we’ll remind you later about this approach.
在程序运行的时候,一般会一闪而逝,此时如果想让屏幕悬停就要用到getchar()语句,之后按下回车屏幕就会消失。不过在一些具体情况下就需要使用到两个getchar()函数了,比如当程序需要你输入体重的时候,当你输入体重并按下回车的时候,第一个getchar()就会读取按下回车的动作,此时,就需要第二个getchar()发挥屏幕悬停的作用。
Review Questions You’ll find answers to the review questions in Appendix A, “Answers to the Review Questions.”
1. What does portability mean in the context of programming?
2. Explain the difference between a source code file, object code file, and executable file.
3. What are the seven major steps in programming?
4. What does a compiler do?
5. What does a linker do?
Programming Exercise We don’t expect you to write C code yet, so this exercise concentrates on the earlier stages of the programming process.
1. You have just been employed by MacroMuscle, Inc. (Software for Hard Bodies). The company is entering the European market and wants a program that converts inches to centimeters (1 inch = 2.54 cm). The company wants the program set up so that it prompts the user to enter an inch value. Your assignment is to define the program objectives and to design the program (steps 1 and 2 of the programming process).
My Answers:
1、程序(源代码不经修改)可以在一个平台上编译运行,相同的程序也可以在其他平台上编译运行
2、source code是Programmer编写好的代码
object code是编译器将source code向executable file编译的过程中产生的文件,当编译完成后,object code file就会被删除An object code file contains machine language code; it need not be the code for a complete program.
executable file是编译好的完整的.exe文件,由机器语言组成可以双击直接运行
3:
1、定义程序的目标
2、设计程序
3、写代码
4、编译
5、运行程序
6、测试并调试程序
7、维护并修改程序
4、将源代码编译成可执行的.exe文件
A compiler translates source code (for example, code written in C) to the equivalent machine language code, also termed object code.
把Programmer写好的source code translates成相对应的机器语言,相当于object code
5、在编译过程中,从library获取相应的code,参与编译过程
The linker combines translated source code with library code and startup code to produce an executable program.
参考答案
Answers to Review Questions for Chapter 1
1. A perfectly portable program is one whose source code can, without modification, be compiled to a successful program on a variety of different computer systems.
2. A source code file contains code as written in whatever language the programmer is using. An object code file contains machine language code; it need not be the code for a complete program. An executable file contains the complete code, in machine language, constituting an executable program.
3. a. Defining program objectives.
b. Designing the program.
c. Coding the program.
d. Compiling the program.
e. Running the program.
f. Testing and debugging the program.
g. Maintaining and modifying the program.
4. A compiler translates source code (for example, code written in C) to the equivalent machine language code, also termed object code.
5. The linker combines translated source code with library code and startup code to produce an executable program.