Unit 2 Software Knowledge

2.1 C Language

2.1.1 Text

C is a general-purpose, structured programming language. Its instructions consist of terms that resemble algebraic expressions, augmented by certain English keywords such as if, else, for, do and while. In this respect C resembles other high-level structured programming languages. C also contains certain additional features however, that allow it to be used at a lower level, thus bridging the gap between machine language and the more conventional high-level languages. This flexibility allows C to be used for systems programming (e.g., for writing operating systems) as well as for applications programming (e.g., for writing a program to solve a complicated system of mathematical equations, or for writing a program to bill customers).

C was developed in the early 1970s. C might best be described as a “medium-level language.”Like a true high-level language, there is a one-to-many relationship between a C statement and the machine language instructions it is complied into. Thus, a language like C gives you far more programming leverage than a low-level assembly language. However, compared to most high-level language, C has a very small set of constructs.However, compared to most high-level language, C has a very small set of constructs. 本句中的 “compared to”的含义是“将……与……比较”。译文:与大多数高级语言相比,C语言有一个小的结构集。 In addition, unlike most high-level language, C lets you easily do chores (such as bit and pointer manipulation) additionally performed by assembly language.

Structured language

Although the term block-structured language does not strictly apply to C in an academic sense, C is informally part of that language group. The distinguishing feature of block-structured language is that the compartmentalization of code and data. This means that a language can section off and hide from the rest of the program all information and instructions that are necessary to perform a specific task. Generally, compartmentalization is achieved by subroutines with local, or temporary, variables. In this way, you can write subroutines so that the events that occur within them will cause no side effects in other parts of the program. Excessive use of global variables, which are known throughout the entire program, may allow bugs, or unwanted side effects, to creep into a program. In C, all subroutines are discrete functions.

Functions are the building blocks of C, in which all program activity occurs. They allow you to define and code specific tasks in a program separately. After debugging a function that uses only local variables, you can rely on it to work properly in various situations without creating side effects in other parts of your program. All variables that are declared in that function will be known only to that function.

A C program consists of a series of functions. Program execution must begin with a function called main ( ). Other functions included in the program are named by the programmer. When the name of a function appears as a statement in a program, program execution transfers to that function. After the called function has been completely executed, a result is made to the calling function. If one function calls another function, the second function is said to be nested inside the first. In many cases, when a function is called, information is passed to it. This information is included within the parentheses after the function name. The called function may also return a single value to the calling function.

Declarations and Definitions

The difference between a declaration and a definition in C is subtle but important. A declaration associates a data type with an identifier but does not actually allocate any storage for it. A definition, on the other hand, actually allocates memory. The distinction between declarations and definitions is particularly important when creating global variables.

A “global variable” (also called an “external variable”) is one that can be accessed by modules in different source files; that is, a global variable has global scope. There are three ways that you create a global variable:

· Create a variable at the top-level with the extern storage class specifier. This produces a variable declaration. Such a declaration cannot contain an initializer. You can access this global variable throughout the remainder of the file.

· Create a variable at the head-of block with the extern storage class specifier. This produces a variable declaration. Such a declaration cannot contain an initializer. You can access this variable only within the block in which you declare it.

· Create a variable at the top-level and omit a storage class specifier. This produces a variable definition. Such a definition can contain an optional initializer.

In C language, every global variable can be declared zero or more times (in different files), but must be defined at least once, and may be defined more than once in different files. You cannot, however, define a global variable more than once in the same file. If you explicitly initialize a global variable in more than one file, the last initializer read by the binder is the variable’s initial value at runtime. Therefore, the order in which you list files in the bind command determines the initial values of external variables. If you do not initialize a global definition, its initial value defaults to zero.

The duration of a variable is the period of time during which storage is allocated to the variable. There are two categories of duration: dynamic and fixed. A variable with dynamic duration is created anew each time the block in which it is declared is entered.A variable with dynamic duration is created anew each time the block in which it is declared is entered. 本句中的“in which it is declared”是定语从句,修饰和限定“the block”。而“each time the block in which it is declared is entered”是一个时间状语从句,修饰“is created”。译文:每当程序进入变量说明块时,一个动态持续变量就会重新建立。 When the program leaves the block, the variable disappears. Conversely, a variable with fixed duration exists throughout the execution of the entire program.

We can summarize the differents between fixed and dynamic variables as follows:

· Fixed variables maintain their values from one block invocation to another, but dynamic variables lose their value each time the block is deactivated.

· Fixed variables get a default initialization value of zero if you do not explicitly initialize them. However, if you do not explicitly initialize a dynamic variable, the compiler will not initialize it for you.

· The run-time system initializes fixed variables only once, whereas dynamic variables, if they are declared with an initializer, are re-initialized each time their block is entered.The run-time system initializes fixed variables only once, whereas dynamic variables, if they are declared with an initializer, are re-initialized each time their block is entered. 本句中if引导了一个条件状语从句。“whereas”是一连词,含义是“反之;而”。译文:实时系统只初始化一次固定变量,而对于动态变量,若用初始化程序说明,则每当进入动态变量块时,就重新初始化。

The scope of a variable is the region in source code over which the variable is active. If a variable is active, it means that you can use it in your source code. If a variable is not active, and you attempt to use it in your source code, the compiler issues an error.

There are four types of scope: block, function, file, and program. Block scope means that the variable is active from its declaration point to the end of the block in which it is declared. Function scope means that the variable is active from its declaration point to the end of the function. File scope means that variable is active from its declaration point to the end of the file. Global scope means that the variable is active for the entire program (including all the files of source code that comprise the program).

Characteristics

C is characterized by the ability to write very concise source programs, due in part to the large number of operators included within the language. It has a relatively small instruction set, though actual implementations include extensive library functions which enhance the basic instructions. Furthermore, the language encourages users to write additional library functions of their own. Thus, the features and capabilities of C language can easily be extended by the user.

C compilers are commonly available for computers of all sizes, and C interpreters are becoming increasingly common.C compilers are commonly available for computers of all sizes, and C interpreters are becoming increasingly common. 本句是一个并列句,“C compilers”和“C interpreters”是主语。译文:C语言的编译程序普遍适用于各种容量的计算机,并且C语言的解释程序正变得越来越普通。 The compilers are usually compact, and they generate object programs that are small and highly efficient when compared with programs compiled from other high-level languages, the interpreters are less efficient, though they are easier to use when developing a new program. Many programmers begin with an interpreter, and then switch to a compiler once the program has been debugged (once all of the programming errors have been removed).

Another important characteristic of C is that its programs are highly portable, even more so than with other high-level languages. The reason for this is that C relegates most computer-dependent features to its library functions. Thus, every version of C is accompanied by its own set of library functions, which are written for the particular characteristics of the host computer.Thus, every version of C is accompanied by its own set of library functions, which are written for the particular characteristics of the host computer. 本句中的“which are written for…”作为非限定性定语从句,修饰“library functions”。译文:因此,每个版本的C语言都伴有它自己的库函数集,这些库函数集是按主机的特点而编写的。 These library functions are relatively standardized, however, and each individual library function is generally accessed in the same manner from one version of C to another. Therefore, most C programs can be processed on many different computers with little or no alteration.

Key Words

academic 学术的

activity 活动,活跃

category 种类,类项

compartmentalization 划分,分门别类

compiler 编译器

conventional 传统的,习惯的

creep 蔓延,爬行

debug 调试

declaration 声明

definition 定义

duration 持续时间,持续

flexibility 灵活性,适应性

identifier 标识符

interpreter 解释器

leverage 杠杆作用

parentheses 括号

pointer 指针

statement 语句

subroutine 子程序

subtle 微小的

2.1.2 Exercises

1.Translate the following phrases into English

(1)高级语言

(2)源代码

(3)动态变量

(4)顶层

(5)全局变量

(6)一对多

(7)数学公式

(8)库函数

2.Translate the following phrases into Chinese

(1)structured programming language

(2)machine language

(3)local variable

(4)external variable

(5)a called function

(6)a calling function

(7)run-time system

(8)assembly language

3.Identify the following to be True or False according to the text

(1)There is a one-to-one relationship between a C statement and a machine language instruction.

(2)A C program must begin with a function called main ( ).

(3)In C language, the called function may return a single value to the calling function.

(4)There is no difference between a declaration and a definition in C language.

(5)In C language, every global variable can only be declared one time.

(6)The duration of a variable is the period of time during which storage is allocated to the variable.

(7)The scope of a variable is the region in source code over which the variable is active.

(8)Most C programs can be processed on many different computers with little or no alteration.

4.Reading Comprehension

(1)____________________ maintain their values from one block invocation to another.

a.Local variables

b.Variables

c.Dynamic variables

d.Fixed variables

(2)If a variable is active, it means that you can use it in your ______________.

a.source code

b.object code

c.compiler

d.interpreter

(3)C was developed in the early _____________.

a.1970s

b.1950s

c.1980s

d.1860s

(4)After the called function has been completely executed, a result is made to the______________.

a.called function

b.main function

c.function

d.calling function

2.1.3 Reading Material

Programming Languages

A Little Terminology

Computer program—A computer program is a set of instructions that tells a computer exactly what to do. The instructions might tell the computer to add up a set of numbers, or compare two numbers and make a decision based on the result, or whatever. But a computer program is simply a set of instructions for the computer. The computer follows your instructions exactly and in the process does something useful—like balancing a checkbook or displaying a game on the screen or implementing a word processor.

Programming language—In order for a computer to recognize the instructions you give it, those instructions need to be written in a language the computer understands—a programming language. There are many computer programming languages—Fortran, Cobol, Basic, Pascal, C, C++, Java, Perl—just like there are many spoken languages. They all express approximately the same concepts in different ways.

Compiler—A compiler translates a computer program written in a human-readable computer language into a form that a computer can execute. You have probably seen EXE files on your computer. These EXE files are the output of compilers. They contain machine-readable programs translated from human-readable programs.

Procedural programming—Procedural programming involves using your knowledge of a programming language to create computer memory locations that can hold values and writing a series of steps or operations that manipulate those values. The computer memory locations are called variables because they hold values that might vary. For convenience, the individual operations used in a computer program often are grouped into logical units called procedures. A procedural program defines the variable memory locations and then calls or invokes a series of procedures to input, manipulate, and output the values stored in those locations. A single procedural program often contains hundreds of variable and thousands of procedure calls.

Object-oriented programming—Object-oriented programming is an extension of procedural programming in which you take a slightly different approach to writing computer programs. Thinking in an object-oriented manner involves envisioning program components as objects that are similar to concrete objects in the real world. Then you manipulate the objects to achieve a desired result. Writing object-oriented programs involves both creating objects and creating applications that use those objects.

Machine language and assembly language

Computer programs that can be run by a computer’s operating system are called executables. An executable program is a sequence of extremely simple instructions known as machine code. These instructions are specific to the individual computer’s CPU and associated hardware. Machine code instructions are few in number (depending on the computer and the CPU). Typical instructions are for copying data from a memory location or for adding the contents of two memory locations (usually registers in the CPU). Machine code instructions are binary—that is, sequences of bits (0s and 1s).

Assembly language uses commands that are easier for programmers to understand than machine-language commands. Each machine language instruction has an equivalent command in assembly language. For example, in assembly language, the statement “MOV A, B” instructs the computer to copy data from one location to another. The same instruction in machine code is a string of 16 0s and 1s. Once an assembly-language program is written, it is converted to a machine-language program by another program called an assembler. Assembly language is fast and powerful because of its correspondence with machine language. It is still difficult to use, however, because assembly-language instructions are a series of abstract codes. In addition, different CPUs use different machine languages and therefore require different assembly languages. Assembly language is sometimes inserted into a high-level language program to carry out specific hardware tasks or to speed up a high-level program.

High-Level Languages

A high-level programming language is a means of writing down, in formal terms, the steps that must be performed to process a given set of data in a uniquely defined way. It may bear no relation to any given computer but does assume that a computer is going to be used. The high-level languages are often oriented toward a particular class of processing problems. For example, a number of languages have been designed to process problems of a scientific—mathematic nature, and other languages have appeared that emphasize file processing applications.

Object-oriented programming (OOP) languages like C++ are based on traditional high-level languages, but they enable a programmer to think in terms of collections of cooperating objects instead of lists of commands. Classes of objects can inherit features from other classes of objects.For example, a class defining squares can inherit features such as right angles from a class defining rectangles. This set of programming classes simplifies the programmer’s task, resulting in more reliable and efficient programs. Software reliability can be improved by object-oriented programming. Since the objects are repeatedly tested in a variety of applications, bugs are more likely to be found and corrected. Object-oriented programming also has potential benefits in parallel processing. Execution speed under object oriented methods will improve with parallel processing.