[计算机软件及应用]c语言教程 C Tutorial.doc

上传人:sccc 文档编号:4561536 上传时间:2023-04-27 格式:DOC 页数:658 大小:1.63MB
返回 下载 相关 举报
[计算机软件及应用]c语言教程 C Tutorial.doc_第1页
第1页 / 共658页
[计算机软件及应用]c语言教程 C Tutorial.doc_第2页
第2页 / 共658页
[计算机软件及应用]c语言教程 C Tutorial.doc_第3页
第3页 / 共658页
[计算机软件及应用]c语言教程 C Tutorial.doc_第4页
第4页 / 共658页
[计算机软件及应用]c语言教程 C Tutorial.doc_第5页
第5页 / 共658页
点击查看更多>>
资源描述

《[计算机软件及应用]c语言教程 C Tutorial.doc》由会员分享,可在线阅读,更多相关《[计算机软件及应用]c语言教程 C Tutorial.doc(658页珍藏版)》请在三一办公上搜索。

1、Node:Top, Next:Preface, Previous:(dir), Up:(dir)C Programming Tutorial (K&R version 4)This is a C Programming Tutorial for people who have a little experience with an interpreted programming language, such as Emacs Lisp or a GNU shell.Edition 4.02Copyright 1987,1999 Mark BurgessPermission is granted

2、 to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies. Preface: Introduction: Reserved words & example: Operating systems: Libraries: Programming style: Form of a C program: Comments: Functions: Variables: Paramete

3、rs: Scope: Preprocessor: Pointers: Standard Output and Standard Input: Assignments Expressions and Operators: Decisions: Loops: Arrays: Strings: Putting together a program: Special Library Functions and Macros: Hidden Operators: More on Data Types: Machine Level Operations: Files and Devices: Struct

4、ures and Unions: Data structures: Recursion: Example Programs chapter: Errors and debugging: Summary: reserved words list: Comparisons: Character Conversion Table: Emacs style file: Answers to questions: Index:Node:Preface, Next:Introduction, Previous:Top, Up:TopPrefaceEvery program is limited by th

5、e language which is used to write it. C is a programmers language. Unlike BASIC or Pascal, C was not written as a teaching aid, but as an implementation language. C is a computer language and a programming tool which has grown popular because programmers like it! It is a tricky language but a master

6、ful one. Sceptics have said that it is a language in which everything which can go wrong does go wrong. True, it does not do much hand holding, but also it does not hold anything back. If you have come to C in the hope of finding a powerful language for writing everyday computer programs, then you w

7、ill not be disappointed. C is ideally suited to modern computers and modern programming.This book is a tutorial. Its aim is to teach C to a beginner, but with enough of the details so as not be outgrown as the years go by. It presumes that you have some previous aquaintance with programming - you ne

8、ed to know what a variable is and what a function is - but you do not need much experience. It is not essential to follow the order of the chapters rigorously, but if you are a beginner to C it is recommended. When it comes down to it, most languages have basically the same kinds of features: variab

9、les, ways of making loops, ways of making decisions, ways of accessing files etc. If you want to plan your assault on C, think about what you already know about programming and what you expect to look for in C. You will most likely find all of those things and more, as you work though the chapters.T

10、he examples programs range from quick one-function programs, which do no more than illustrate the sole use of one simple feature, to complete application examples occupying several pages. In places these examples make use of features before they have properly been explained. These programs serve as

11、a taster of what is to come.Mark Burgess. 1987, 1999This book was first written in 1987; this new edition was updated and rewritten in 1999. The book was originally published by Dabs Press. Since the book has gone out of print, David Atherton of Dabs and I agreed to release the manuscript, as per th

12、e original contract. This new edition is written in Texinfo, which is a documentation system that uses a single source file to produce both on-line information and printed output. You can read this tutorial online, using either the Emacs Info reader, the standalone Info reader, or a World Wide Web b

13、rowser, or you can read this same text as a typeset, printed book.Node:Introduction, Next:Reserved words & example, Previous:Preface, Up:TopIntroductionWhat is C? What is it for? Why is it special? Levels: Basic ideas: The compiler: Errors: Use of Upper and Lower Case: Questions 1:Node:Levels, Next:

14、Basic ideas, Previous:Introduction, Up:IntroductionHigh Levels and Low LevelsAny kind of object that is sufficiently complicated can be thought of as having levels of detail; the amount of detail we see depends upon how closely we scrutinize it. A computer falls definitely into the category of compl

15、ex objects and it can be thought of as working at many different levels. The termslow levelandhigh levelare often used to describe these onion-layers of complexity in computers. Low level is perhaps the easiest to understand: it describes a level of detail which is buried down amongst the working pa

16、rts of the machine: the low level is the level at which the computer seems most primitive and machine-like. A higher level describes the same object, but with the detail left out. Imagine stepping back from the complexity of the machine level pieces and grouping together parts which work together, t

17、hen covering up all the details. (For instance, in a car, a group of nuts, bolts, pistons can be grouped together to make up a new basic object: an engine.) At a high level a computer becomes a group of black boxes which can then be thought of as the basic components of the computer.C is called a hi

18、gh level, compiler language. The aim of any high level computer language is to provide an easy and natural way of giving a programme of instructions to a computer (a computer program). The language of the raw computer is a stream of numbers called machine code. As you might expect, the action which

19、results from a single machine code instruction is very primitive and many thousands of them are required to make a program which does anything substantial. It is therefore the job of a high level language to provide a new set of black box instructions, which can be given to the computer without us n

20、eeding to see what happens inside them - and it is the job of a compiler to fill in the details of these black boxes so that the final product is a sequence of instructions in the language of the computer.C is one of a large number of high level languages which can be used for general purpose progra

21、mming, that is, anything from writing small programs for personal amusement to writing complex applications. It is unusual in several ways. Before C, high level languages were criticized by machine code programmers because they shielded the user from the working details of the computer, with their b

22、lack box approach, to such an extent that the languages become inflexible: in other words, they did not not allow programmers to use all the facilities which the machine has to offer. C, on the other hand, was designed to give access to any level of the machine down to raw machine code and because o

23、f this it is perhaps the most flexible of all high level languages.Surprisingly, programming books often ignore an important role of high level languages: high level programs are not only a way to express instructions to the computer, they are also a means of communication among human beings. They a

24、re not merely monologues to the machine, they are a way to express ideas and a way to solve problems. The C language has been equipped with features that allow programs to be organized in an easy and logical way. This is vitally important for writing lengthy programs because complex problems are onl

25、y manageable with a clear organization and program structure. C allows meaningful variable names and meaningful function names to be used in programs without any loss of efficiency and it gives a complete freedom of style; it has a set of very flexible loop constructions (for,while,do) and neat ways

26、 of making decisions. These provide an excellent basis for controlling the flow of programs.Another unusual feature of C is the way it can express ideas concisely. The richness of a language shapes what it can talk about. C gives us the apparatus to build neat and compact programs. This sounds, firs

27、t of all, either like a great bonus or something a bit suspect. Its conciseness can be a mixed blessing: the aim is to try to seek a balance between the often conflicting interests of readability of programs and their conciseness. Because this side of programming is so often presumed to be understoo

28、d, we shall try to develop a style which finds the right balance.C allows things which are disallowed in other languages: this is no defect, but a very powerful freedom which, when used with caution, opens up possibilities enormously. It does mean however that there are aspects of C which can run aw

29、ay with themselves unless some care is taken. The programmer carries an extra responsibility to write a careful and thoughtful program. The reward for this care is that fast, efficient programs can be produced.C tries to make the best of a computer by linking as closely as possible to the local envi

30、ronment. It is no longer necessary to have to put up with hopelessly inadequate input/output facilities anymore (a legacy of the timesharing/mainframe computer era): one can use everything that a computer has to offer. Above all it is flexible. Clearly no language can guarantee intrinsically good pr

31、ograms: there is always a responsibility on the programmer, personally, to ensure that a program is neat, logical and well organized, but it can give a framework in which it is easy to do so.The aim of this book is to convey some of the C philosophy in a practical way and to provide a comprehensive

32、introduction to the language by appealing to a number of examples and by sticking to a strict structuring scheme. It is hoped that this will give a flavour of the kind of programming which C encourages.Node:Basic ideas, Next:The compiler, Previous:Levels, Up:IntroductionBasic ideas about CWhat to do

33、 with a compiler. What can go wrong.Using a compiler language is not the same as using an interpreted language like BASIC or a GNU shell. It differs in a number of ways. To begin with, a C program has to be created in two stages: Firstly, the program is written in the form of a number of text files

34、using a screen editor. This form of the program is called thesource program. It is not possible to execute this file directly. Secondly, the completed source file is passed to a compiler-a program which generates a new file containing a machine code translation of the source text. This file is calle

35、d an object file or executable file. The executable file is said to have been compiled from the source text.Compiler languages do not usually contain their own editor, nor do they have words likeRUNwith which to execute a finished program. You use a screen editor to create the words of a program (pr

36、ogram text) and run the final program in its compiled form usually by simply typing the name of the executable file. The compiler: Errors:Node:The compiler, Next:Errors, Previous:Basic ideas, Up:IntroductionThe CompilerA C program is made by running acompilerwhich takes the typed source program and

37、converts it into an object file that the computer can execute. A compiler usually operates in two or more phases (and each phase may have stages within it). These phases must be executed one after the other. As we shall see later, this approach provides a flexible way of compiling programs which are

38、 split into many files.A two-phase compiler works in the following way: Phase 1 scans a source program, perhaps generating an intermediate code (quadruples or pcode) which helps to simplify the grammar of the language for subsequent processing. It then converts the intermediate code into a file of o

39、bject code (though this is usually not executable yet). A separate object file is built for each separate source file. In the GNU C compiler, these two stages are run with the commandgcc -c; the output is one or more.ofiles. Phase 2 is a Linker. This program appends standard library code to the obje

40、ct file so that the code is complete and can stand alone. A C compiler linker suffers the slightly arduous task of linking together all the functions in the C program. Even at this stage, the compiler can fail, if it finds that it has a reference to a function which does not exist. With the GNU C co

41、mpiler this stage is activated by the commandgcc -oorld.To avoid the irritation of typing two or three separate commands (which are often cumbersome) you will normally find a simple interface for executing compiler. Traditionally this is an executable program calledccfor C Compiler:cc filenamegcc fi

42、lenameOn GNU systems, this results in the creation of an executable program with the default namea.out. To tell the compiler what you would like the executable program to be called, use the-ooption for setting the name of the object code:gcc -o program-name filnameFor example, to create a program ca

43、lledmyprogfrom a file calledmyprog.c, writegcc -o myprog myprog.cNode:Errors, Next:Use of Upper and Lower Case, Previous:The compiler, Up:IntroductionErrorsErrors are mistakes which we the programmers make. There are different kinds of error:SyntaxErrors in the syntax, or word structure of a program

44、 are caught before you run it, at compilation time by the compiler program. They are listed all in one go, with the line number, in the text file, at which the error occurred and a message to say what was wrong.For example, suppose you writesin (x) y = ;in a program instead ofy = sin (x);, which ass

45、igns the value of the sin ofxtoy. Upon compilation, you would see this error message:eg.c: In function main:eg.c:12: parse error before y(If you compile the program in Emacs, you can jump directly to the error.)A program with syntax errors will cause a compiler program to stop trying to generate mac

46、hine code and will not create an executable. However, a compiler will usually not stop at the first error it encounters but will attempt to continue checking the syntax of a program right to the last line before aborting, and it is common to submit a program for compilation only to receive a long an

47、d ungratifying list of errors from the compiler.It is a shock to everyone using a compiler for the first time how a single error can throw the compiler off course and result in a huge and confusing list of non-existent errors, following a single true culprit. The situation thus looks much worse than

48、 it really is. Youll get used to this with experience, but it can be very disheartening.As a rule, look for thefirsterror, fix that, and then recompile. Of course, after you have become experienced, you will recognize when subsequent error messages are due to independent problems and when they are due to a cascade. But at the beginning, just look for and fix the first error.IntentionErrors in goal or purpose (logical errors) occur when you write a program that works, but does not do what you intend it to do. You intend to send a

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 教育教学 > 成人教育


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号