C语言程序设计(双语)课件Chapter 1 Introduction to C_.ppt
C Programming LanguageInstructor:Rao HongComputer Center,Nanchang UniversityE-mail:Reference Books1.1.C C程序设计(第二版),谭浩强,清华大学程序设计(第二版),谭浩强,清华大学出版社出版社2.2.C C程序设计题解与上机指导(第二版),谭程序设计题解与上机指导(第二版),谭浩强,清华大学出版社浩强,清华大学出版社Text BookThe Art and Science of C,Eric S.Roberts China Machine PressRao Hong NCU-Autumn 2004The Art and Science of CBackgroundu This course is adopted from a CS106A at Computer Science Department of Stanford University.u Most of the materials(textbook,some handouts,software as well as most assignments)we will use in the class is adopted from CS106A of Stanford.Rao Hong NCU-Autumn 2004The Art and Science of CCS106A at StanfordCS106A is one of the largest coursed at Stanford.Its enrollment now is over 900 a year.Together with 106B,this series teaches you the C programming language,which is widely used in industry.CS106 is explicitly designed to appeal to humanists and social scientists as well as hardcore technology disciplines.The course requires no previous background in programming,but does require considerable dedication and hard work.Rao Hong NCU-Autumn 2004The Art and Science of CGradesThe ordinary score 20%includes the quiz and all the assignments The Final Examination will be 80%of the total score.Rao Hong NCU-Autumn 2004The Art and Science of CTimetableRao Hong NCU-Autumn 2004The Art and Science of CLecture 1Chapter 1 Introduction to CRelated Chapters Sections 1.4-1.9,Sections 2.12.3 ObjectivesTo understand the meaning of the term algorithmTo appreciate the role of the compiler as a translator between a higher-level programming language and the low-level machine languageTo recognize that many simple programs are composed of three phases:input,computation,and output.To understand the role of variables in a program as placeholders for data values.Rao Hong NCU-Autumn 2004The Art and Science of C1.AlgorithmA strategy for solving a problem,must be Clearly and unambiguously definedEffective,in the sense that its steps are executable;Finite,in the sense that it terminates after a bounded number of stepsRao Hong NCU-Autumn 2004The Art and Science of C2.Programming Language and compilation Programming Language Classified asLow Level Machine Language(binary-based code;machine dependent)Rao Hong NCU-Autumn 2004The Art and Science of CProgramming Language Classified asHigh Level Closer to natural languages.Generally,machine independent Usually,several machine instructions are combined into one high-level instruction.Examples:FORTRAN COBOLBASIC JavaPascal Ada PL/I LispC GPSSC+Matlab Rao Hong NCU-Autumn 2004The Art and Science of CPrograms written in high-level languages must be converted to machine language.Two approaches:(1)Compilation(see p.12 Figure1-2)Used with C,C+,Fortran,.(2)InterpretationUsed with Matlab,Visual Basic,.Processing a High-level Language ProgramRao Hong NCU-Autumn 2004The Art and Science of CThe Compilation Process#include main()printf(“hellon”);110111100101111010001010110010101001010101010010011100011101111001011110100010101100101010010101011010001010110010101001010101compilerlinkerExecutable fileObject FileOther Object Files/LibrariesSource FileRao Hong NCU-Autumn 2004The Art and Science of CStep 1)Use TC to create a“source”file.We will name source files with a suffix“.c”Step 2)Run the compiler on the source file to create an object file and executable file with the default name a.exe and a.obj.(Ctrl+F9)Step 3)Check to see if compilation caught any syntax errors,if so go back to step 1)Step 4)Check the result through Alt+F5 at the full screen.CompilationRao Hong NCU-Autumn 2004The Art and Science of CTurbo CStep 1:Create source fileRao Hong NCU-Autumn 2004The Art and Science of CStep 2:Run the Complier(or Ctrl+F9)Rao Hong NCU-Autumn 2004The Art and Science of CStep 3:Check the ResultAlt+F5Rao Hong NCU-Autumn 2004The Art and Science of CVisual C+Create a new projectRao Hong NCU-Autumn 2004The Art and Science of C3.Programming errors and debuggingSyntax errors:Errors that result from breaking syntactic rules.Bug:Mistakes that result from the wrong logic of the program.Debugging:The process of finding and correcting such mistakes.Rao Hong NCU-Autumn 2004The Art and Science of CChapter 2Learning by Example1.The“Hello world“program /*File:hello.c*-*This program prints the message“Hello,world.”*on the screen.The program is taken from the classic C*reference text“The C programming Language”by*Brian Kernighan and Dennis Ritchie.*/#include#include“genlib.h”main()printf(“Hello,world!.n);library inclusionmain programprogram commentRao Hong NCU-Autumn 2004The Art and Science of CLibrary inclusions#includeANSI C standard libraries are marked with angle brackets#include“genlib.h”Personal libraries are specified using quotation marks.There is no such library in the ANSI C.Rao Hong NCU-Autumn 2004The Art and Science of Cmain program#include.main()./*function body*/.a“block”in C is defined by a set of curly braces.Rao Hong NCU-Autumn 2004The Art and Science of CA program to add two numbers/*File:add2.c*-*This program reads in two numbers,adds them together,*and prints their sum.*/#include/*#include genlib.h*#include simpio.h“*/main()int n1,n2,total;printf(This program adds two numbers.n);printf(1st number?);scanf(“%d”,&n1);/*n1=GetInteger();*/printf(2nd number?);scanf(“%d”,&n2);/*n2=GetInteger();*/total=n1+n2;printf(The total is%d.n,total);promptinputcomputationoutputdeclare the variableRao Hong NCU-Autumn 2004The Art and Science of CInput phrase25n1n2scanf(“%d”,&n1);scanf(“%d”,&n2);Rao Hong NCU-Autumn 2004The Art and Science of CThe computation phasetotal=n1+n2;The result of the expression is stored in a variable using an assignment statement.Rao Hong NCU-Autumn 2004The Art and Science of CThe output phaseprintf(The total is%d.n,total)The%d format code means that the output should be displayed as a decimal integerformat codeRao Hong NCU-Autumn 2004The Art and Science of CPerspectives on the programming process Look at the main program for the add2.c program again and try to express in one sentence what it does:main()int n1,n2,total;printf(This program adds two numbers.n);printf(1st number?);scanf(“%d”,&n1);/*n1=GetInteger();*/printf(2nd number?);scanf(“%d”,&n2);/*n2=GetInteger();*/total=n1+n2;printf(The total is%d.n,total);The program adds two numbers and display the result.Rao Hong NCU-Autumn 2004The Art and Science of CYou can perceive a program in either reductionistic approach or in holistic approach.Rao Hong NCU-Autumn 2004The Art and Science of CAssignment:Be Familiar with the Turbo C environment and the menu of C or Visual C+Programming Exercises:P.55 No4,5Review Questions:P.17 NO.10,12,13Rao Hong NCU-Autumn 2004The Art and Science of C