C语言程序设计 第二章 C语言基本概念.1

This chapter introduces several basic concepts, including preprocessing directives,functions, variables, and statements, that we'll need in order to write even the sim-plest programs. Later chapters will cover these topics in much greater detail.

本章介绍了C语言的一些基本概念,包括预处理指令(preprocessor directive)、函数(function)、变量(variable)和语句(statement)。即使是编写最简单的C程序,也会用到这些基本概念。后续的几章将会对这些概念进行进一步的详细描述。


To start off, Section 2.1 presents a small C program and describes how to compile and link it. Section 2.2 then discusses how to generalize the program, and Section 2.3 shows how to add explanatory remarks, known as comments. Section 2.4 introduces variables, which store data that may change during the execution of a program, and Section 2.5 shows how to use the scanf function to read data into variables. Constants-data that won't change during program execution-can be given names. as Section 2.6 shows. Finally. Section 2.7 explains C's rules for creating names (identifiers) and Section 2.8 gives the rules for laying out a program.

首先,2.1节给出一个简单的C程序,并且描述了如何对程序进行编译和链接。接着,2.2节讨论如何使程序通用。2.3节说明如何添加说明性解释,即通常所说的注释(Comment)。2.4节介绍变量,变量是用来存储程序执行过程中可能会发生改变的数据的。2.5节说明利用scanf函数把数据读入变量的方法。就如2,6节介绍的那样,常量是程序执行过程中不会发生改变的数据,用户可阻对其进行命名。最后,2.7节解释C语言的命名(标识符(identifier))规则,2.8节给出了C程序的书写规范。


2.1  Writing a Simple Program

In contrast to programs written in some languages. C programs require little "boilerplate"-a complete program can be as short as a few lines.

与用其他语言编写的程序相比,C程序较少要求“死板的格式”。一个完整的C程序可以只有寥寥数行


PROGRAM   Printing a Pun

The first program in Kernighan and Ritchie's classic The C Programming Language is extremely short; it does nothing but write the message hello, world.Unlike other C authors, I won't use this program as my tirst example. I will, however. uphold another C tradition: the bad pun. Here's the pun:

在Kemighan和Ritchie编写的经典The C Programming Language一书中,第一个程序是极其简短的。它仅仅输出了一条hello,world信息。与大多数C语言书籍的作者不同,这里不打算用这个程序作为第一个C程序示例,反倒是更愿意支持另一种C语言的传统:双关语。下面是一条双关语


To C, or not to C: that is the question.


The following program, which we'll name pun.c,displays this message each time it is run.

pun.c  

#include <stdio.h>

int  main (void)

{

  printf("To C, or not to C: that is the question.\n");

  return 0;

}


Section 2.2 explains the form of this program in some detail. For now. I'll just make a few brief observaions. The line

2.2节会对这段程序中的一些格式细节进行详尽的说明,这里仅做一个简明扼要的介绍。程序中第一行


#include <stdio.h>


is necessary to "include" information about C's standard I/O (input/output) library.The program's executable code goes inside main. which represents the "main"program. The only line inside main is a command to display the desired message.printf is a function from the standard I/O library that can produce nicely formatted output. The \n code tells printf to advance to the next line after printing the message. The line

是必不可少的,它“包含”了C语言标准输入/输出库的相关信息。程序的可执行代码都在main函数中,这个函数代表“主”程序。maln函数中唯一的一行代码是用来显示期望的信息的。printf函数来自标准输入/输出库,可以产生完美的格式化输出。代码\n说明printf函数执行完消息显示后要进行换行操作。


return 0;


indicates that the program "returns" the value 0 to the operating system when it terminates.

当程序结束的时候会向操作系统返回0。


Compiling and Linking

Despite its brevity. getting pun . c to run is more involved than you might expect.First, we need to create a file named pun . c containing the program (any text editor will do). The name of the file doesn't matter, bur the .c extension is often required by compilers.

尽管pun.c程序十分简短,但是为运行此程序而包含的内容可能比想象的要多。首先,需要生成一个名为pun.c并且含有上述程序代码的文件(任何一种文本编辑器都可以创建该文件)。文件的名字无关紧要,但是编译器往往要求文件的扩展名是.c。


Next, we've got to convert the program to a form that the machine can execute. For a C program. that usually involves three steps:

接下来,就需要把程序转化为机器可以执行的形式。对于C程序来说,通常包含下列3个步骤:


●Preprocessing. The program is first given to a preprocessor, which obeys commands that begin with # (known as directives). A preprocessor is a bit like an editor; it can add things to the program and make modifications.

预处理。首先会把程序送交给预处理器(preprocessor)。预处理器执行以#开头的命令(通常称为指令,directive)。预处理器有点类似于编辑器,它可以给程序添加内容,也可以对程序进行修改。


●Compiling. The modified program now goes to a compiler. which translates it into machine instructions (object code). The program isn't quite ready to run yet, however.

编译。修改后的程序现在可以进入编译器( compiler)了。编译器会把程序翻译成机器指令(即目标代码,object code)。然而,这样的程序还是不可以运行的。


●Linking. In the final step, a linker combines the object code produced by the compiler with any additional code needed to yield a complete executable program. This additional code includes library functions (like printf) that are used in the program.

链接。在最后一个步骤中,链接器(linker)把由编译器产生的目标代码和任何其他附加代码整合在一起,这样才最终产生了完全可执行的程序。这些附加代码包括很多程序中用到的库函数(例如printf函数)。


Fortunately, this process is often automated,so you won't find it too onerous.in fact, the preprocessor is usually integrated with the compiler,  so you probably won't even notice it at work.

幸运的是,上述过程往往是自动实现的,因此人们会发现这项工作不是太艰巨。事实上,由子预处理器通常会和编译器整合在一起,所以人们甚至可能不会注意到它在工作


The commands necessary to compile and link vary, depending on the compiler and operating system. Under UNIX. the C compiler is usually named cc. To compile and link the pun.c program, enter the following command in a terminal or command-line window:

根据编译器和操作系统的不同,编译和链接所需的命令也是多种多样的。在UNIX系统环境下,通常把C语言编译器命名为cc。为了编译和链接pun.c程序,需要录入如下命令:


% cc pun.c


(The % character is the UNIX prompt, not something that you need to enter.) Linking is automatic when using cc; no separate link command is necessary.

(字符%是UNIX系统的提示符。)在使用编译器cc时,系统自动进行链接操作,而无需单独的链接命令。


After compiling and linking the program, cc leaves the executable program in a file named a . out by default. cc has many options; one of them (the -o option)allows us to choose the name of the file containing the executable program. For example, if we want the executable version of pun . c to be named pun, we woulcl enter the following command:

在编译和链接好程序后,编译器cc会把可执行程序放到默认名为a.out的文件中。编译器cc有许多选项。其中一个选项(一。选项)允许为含有可执行程序的文件选择名字。例如,假设要把文件pun.c生成的可执行文件命名为pun,那么只需录入下列命令


% cc -o pun pun.c


C语言程序设计 第二章 C语言基本概念.1

Integrated Development Environments

集成开发环境


So far, we've assumed the use of a "~command-line" compiler that's invoked by entering a command in a special window provided by the operating system. The alternative is to use an integrated development environment (IDE), a software package that allows us to edit, compile, link. execute. and even debug a program without leaving the environment, The components of an IDE are designed to work together. For example, when the compiler detects an error in a program, it can arrange for the editor to highlight the line that contains the error. There's a great deal of variation among IDEs, so I won't discuss them further in this book. However, I would recommend checking to see which IDEs are available for your platform.

到目前为止,我们一直讨论的都是在操作系统下提供的窗口中输入指令来触发命令行编译。现在我们来讨论集成开发环境这种让我们编辑,编译,链接,运行,在环境调试的软件包,例如,当编译器检测到程序中的错误,它可以让编辑器高亮处理含错误的代码行。尽管有很多种类的集成开发环境,在这本书中不再予以讨论。但是,我依然建议读者好好考虑哪种IDE更适合自己的开发平台。


上一章C语言程序设计 第一章C语言概述.4