C语言讲义第四章.ppt_第1页
C语言讲义第四章.ppt_第2页
C语言讲义第四章.ppt_第3页
C语言讲义第四章.ppt_第4页
C语言讲义第四章.ppt_第5页
已阅读5页,还剩64页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure Part6,Chapter4,Page4-1,Function ,Note: any expression can be followed return statement expression will be converted to the return_type parentheses(optional ) are often used around 表达式的数据类型将被强制转换为函数的返回类型 表达式一般放在一对圆括号中,CHAPTER 4:

2、Function and Program structure,C PROGRAMMING LANGUAGE,E.E.BUAA,Page4-7,No return value,void printstar ( ) printf(“*”); return ; ,Note: use void key word to declare the function which no return value. (使用void关键字表示函数没有返回值) if a function have no argument, also use void to declare the argument. (如果函数没有参

3、数,也可以使用void进行声明),CHAPTER 4:Function and Program structure,C PROGRAMMING LANGUAGE,E.E.BUAA,Page4-8,No Argument Function,return_type function_name ( ) declarations and statements ,for example:,int dummy( ) . ,CHAPTER 4:Function and Program structure,C PROGRAMMING LANGUAGE,E.E.BUAA,Page4-9,Function wit

4、h Argument,return_type function_name ( declaration of each argument) declarations and statements ,int max(int x, int y) return (xy)?x:y; ,int max(x,y) int x,y; return (xy)?x:y; ,在括号中对每一个参数独立的进行声明,在括号中给出参数变量的序列,随后进行声明,CHAPTER 4:Function and Program structure,C PROGRAMMING LANGUAGE,E.E.BUAA,Page4-10,C

5、alling,syntax: function_name(actual argument list),function statement:函数构成的语句 printstar( ); function expression:函数构成的表达式 c=max(a,b); function argument: 函数作为参数c=max(a,max(b,c); printf(“%d”,max(5,8);,format,实参序列,CHAPTER 4:Function and Program structure,C PROGRAMMING LANGUAGE,E.E.BUAA,Page4-11,Data com

6、munication between functions,return_type function_name (argument declaration) declarations and statements return expression; ,Data communication between functions: by arguments;通过参数 by value return by function;通过返回值 by external variables; 通过外部变量,CHAPTER 4:Function and Program structure,C PROGRAMMING

7、 LANGUAGE,E.E.BUAA,Page4-12,Data communication between functions,main( ) int a,b; a=10; b=5; printf(“%d”,max(a,b); ,int max(int x, int y) return (xy)?x:y; ,formal arguments,actual arguments,In declaration, the argument named formal argument(形参) when call function, the argument named actual argument(

8、实参) The formal and actual argument have same type (形参和实参应具有相同的数据类型),CHAPTER 4:Function and Program structure,C PROGRAMMING LANGUAGE,E.E.BUAA,Page4-14,Data communication between functions,main( ) ./* a=5,b=10*/ printf(“%d”,max(a,a+b); . ,int max(int x, int y) return (xy)?x:y; ,notice the directions 单

9、向 值 传递,x+:y+;,函数调用,函数定义,CHAPTER 4:Function and Program structure,C PROGRAMMING LANGUAGE,E.E.BUAA,Page4-15,Function declaration(函数的声明),syntax: return_type function_name( ),main() int max(); . int max(int x, int y) return (xy)?x:y; ,int max(); main() . int max(int x, int y) . ,CHAPTER 4:Function and P

10、rogram structure,C PROGRAMMING LANGUAGE,E.E.BUAA,Page4-16,Function declaration,syntax: return_type function_name( ),need not declaration: return type is int or char; function defined before used; 有两种情况函数不需要声明: 1、函数类型为整型或字符型 2、函数定义在使用之前,CHAPTER 4:Function and Program structure,C PROGRAMMING LANGUAGE,

11、E.E.BUAA,Page4-17,Function declaration,void printstar( ) . . main( ) . printstar( ); .,main( ) . c=max(a,b); . . int max(int x,int y) . ,CHAPTER 4:Function and Program structure,C PROGRAMMING LANGUAGE,E.E.BUAA,Page4-18,Lib function declaration,syntax: #include ,#include main() . printf(.); ,#include

12、 main() . for(i=0;isspace(si);i+) . for( ;isdigit(si);i+) . ,Back,CHAPTER 4:Function and Program structure,C PROGRAMMING LANGUAGE,E.E.BUAA,Page4-19,4.2 Function returning non-integers,#include double atof(char s ) double val,power; int i,sign; for(i=0;isspace(si);i+) ; sign=(si=-)?-1:1; if(si=+|si=-

13、) i+; for(val=0.0; isdigit(si); i+) val=10.0*val+(si-0);,if(si=.)i+; for(power=1.0;isdigit(si);i+) val=10.0*val+(si-0); power*=10; return sign*val/power; ,main() double sum, atof( ); . sum+=atof(s); ,CHAPTER 4:Function and Program structure,C PROGRAMMING LANGUAGE,E.E.BUAA,Page4-20,note: the type dec

14、lared and defined must be consistent. 声明的数据类型和定义的数据类型须一致。 retuned values type = functions type,otherwise convert to functionss type. 返回值的数据类型应当与函数的数据类型一致,否则转换为函数的数据类型,int atoi (char s ) double atof(char s ); return (int)atof(s); ,Back,CHAPTER 4:Function and Program structure,C PROGRAMMING LANGUAGE,E

15、.E.BUAA,Page4-21,4.3 External Variables,External Variables ( Global ): 外部变量(全局变量) defined outside of the function 在函数外部定义的变量称为外部变量。外部变量不属于 任何一个函数,其作用域是:从外部变量的定义位置开 始,到本文件结束为止。 Internal Variables ( Local or Automatic ): 内部变量(局部变量或自动变量) defined inside of the function 定义在函数内部,只在函数内起作用,其他函数不能使用,CHAPTER

16、4:Function and Program structure,C PROGRAMMING LANGUAGE,E.E.BUAA,Page4-22,Internal Variables (automatic variables),1 The variable defined in main,is accessible only in main.主函数内定义的变量只在主函数中有效 The variables which separately defined in different function, can have same name. 不同函数内定义的变量可以有相同的变量名 3 Forma

17、l variables for function are internal variables. 函数的形参变量是内部变量 The variables can be defined in a block. 变量也可以定义在程序块(组)中,CHAPTER 4:Function and Program structure,C PROGRAMMING LANGUAGE,E.E.BUAA,Page4-21,Internal Variables,func1(int x,int y) . func2(int x,int y) .,main() auto int x,y; . func (int x,int

18、 y) .,main() int x,y; . int c; c=x+y; . . ,CHAPTER 4:Function and Program structure,C PROGRAMMING LANGUAGE,E.E.BUAA,Page4-21,External Variables,defined outside of any function (every function is external one) 定义在任何函数之外 be accessible by other function in same source program file 可以被同一文件中的其他函数访问 used

19、to data communication between functions, return more values.可用于函数间的数据交换 if external variable and internal variable have same name, then inside of the function, internal variable available,and outside of the function external variable available; 如果外部变量与内部变量同名,则在函数体内,内部变量起作用,函数体外,外部变量起作用,Back,CHAPTER

20、4:Function and Program structure,C PROGRAMMING LANGUAGE,E.E.BUAA,Page4-22,4.4 Scope Rules,The scope (作用域)of a name is the part of the program within which the name can be used. The scope for automatic is the function which the name is declared. 自动变量或内部变量的作用域为函数体内 The scope of an external variable or

21、 a function lasts from the point at which its declared to the end of the file. 外部变量或函数的作用域为定义之处到该文件的结尾,CHAPTER 4:Function and Program structure,C PROGRAMMING LANGUAGE,E.E.BUAA,Page4-23,Scope Rules,int g_var1;/*external variable*/ int max(int x, int y) . float g_var2; main( ) int a,b,c; int x,y; . ,x

22、,y,a,b,c x,y,g_var1,g_var2,CHAPTER 4:Function and Program structure,C PROGRAMMING LANGUAGE,E.E.BUAA,Page4-24,int g_var1;/*external variable*/ int max(int x, int y) . main( ) int a,b,c; int x,y; . float min(float x1,float x2) . ,x,y,a,b,c x,y,g_var1,x1,x2,min,max,函数的作用域,CHAPTER 4:Function and Program

23、 structure,C PROGRAMMING LANGUAGE,E.E.BUAA,Page4-25,Declare external variable,extern: an external variable is to be referred to before its defined, or if it is defined in a different source file from the one where it is being used. 如果外部变量定义在引用之后,或定义在其他的源文件中,须用extern关键字进行声明。,CHAPTER 4:Function and Pr

24、ogram structure,C PROGRAMMING LANGUAGE,E.E.BUAA,Page4-26,Declare external variable,extern int sp; extern double val; void push(double f). double pop(void).,int sp; double valMAXVAL;,in file1:,in file2:,CHAPTER 4:Function and Program structure,C PROGRAMMING LANGUAGE,E.E.BUAA,Page4-27,Declaration and

25、Definition,Declaration: announces the properties of a variable. 声明: 说明变量的性质 Definition: also causes storage to be set aside. 定义:创建变量的存储空间,int sp;/*both definition and declaration for the rest of the file*/ double val int MAXVAL;,extern int sp;/*declaration only, not create the variables*/ extern dou

26、ble val ; /*its size defined elsewhere;*/,Back,CHAPTER 4:Function and Program structure,C PROGRAMMING LANGUAGE,E.E.BUAA,Page4-28,4.5 Header Files,Header file means one file include other file overall. 文件包含是指,一个源文件可以将另一个源文件的全部内容包含进来。,syntax: #include “filename” or#include ,两种格式的区别仅在于: (1)使用双引号:系统首先到当

27、前目录下查找被包含文件,如果没找到,再到系统指定的“包含文件目录”(由用户在配置环境时设置)去查找。 (2)使用尖括号:直接到系统指定的“包含文件目录”去查找。,CHAPTER 4:Function and Program structure,C PROGRAMMING LANGUAGE,E.E.BUAA,Page4-29,4.5 Header Files,#include “file2”,file1.c,A,B,file2.c,B,A,file1.c,CHAPTER 4:Function and Program structure,C PROGRAMMING LANGUAGE,E.E.BUA

28、A,Page4-30,#include int max(int x, int y) return (xy)?x:y; int min(int ,x int y) return (xy)? y:x; void printstar() printf(“*”);,calc.h,#include “calc.h” main( ) int a,b,c,d; . c=max(a,b); . d=min(a,b); . printf(%d,%d”,c,d); ,Back,work.c,CHAPTER 4:Function and Program structure,C PROGRAMMING LANGUAG

29、E,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-31,4.6 Static Variables,static char bufBUFFerSIZE; static int bufp=0; int getch(void) . void ungetch(int c) .,static: applied to an external variable or function , limits the scope of that object to the rest of the file.静态外部变量只允许被本源文件中的函数引用,f

30、ile1:,extern char buf ; extern int bufp; .,file2:,C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-32,add(int a) int b=0; static int c=3; b+; c+; return a+b+c; ,static: applied to internal variables, provide private, permanent storage. 静态内部变量在程序执行期间始终存在,但其它函数是不能引用它们的。静态

31、内部变量只进行一次赋初值。,file1:,extern add(); main() int a=2; for(i=0;i3;i+) printf(“%dt”, add(a); ,file2:,7 8 9,C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-32,1)需要保留函数上一次调用结束时的值。 2)变量只被引用而不改变其值。,C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-3

32、2,auto: applied to an internal variable 自动局部变量(又称自动变量)只在函数内有效,一般缺省。 auto int x; int x; 函数被调用时分配存储空间,调用结束就释放。在复合语句中定义的自动变量,只在该复合语句中有效;退出复合语句后,也不能再使用,否则将引起错误。,C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-32,Initialize (初始化),Back,int x; static int y;,系统将其赋值为 0或空字符,不确定,

33、C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-33,4.7 Register Variables,Register variables (寄存器变量)are to be placed in machine registers, result in smaller and faster programs.,register int a; register char c;,Note: only internal variables can be declared as register

34、variables. 只有内部变量才可以声明为寄存器变量 2 few registers can be used.寄存器变量的数量是有限的 3 a static variable can not be declared as a register type. 静态变量不能声明为寄存器变量,C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-34,Summery,auto extern static register,internal variable,external variable f

35、unction,automatic variable局部自动变量 static internal variable 局部静态变量 register variable 寄存器变量 formal argument 函数形参,static external variable 全局静态变量 external variable 全局变量,Back,C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-35,4.8 Block Structure,variable can be defined in a

36、 block-structured fashion within a function 程序组-复合语句,if(n0) int i; /* a new variable i, and its scope only inside the block*/ for(i=0;in;i+) . ,Back,C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-37,4.9 Initialization,In the absence of explicit initialization(在进行赋值之前)

37、 external and static variables are guaranteed to be zero(0); 外部变量和静态变量自动赋予0 automatic and register variables have undefined initial value.自动部变量和寄存器变量的初始值不确定,Initialized when defined,int x=1; char squote= ; long day=1000L*60L*60L*24L;,C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program str

38、ucture,Page4-38,For external and static variables the initializer must be a constant expression; the initialization is done once; 初始化为常量表达式,其只有一次初始化,For automatic and register variables 1 the initializer may be any expression involving previously defined values, even function call; 2 the initializat

39、ion is done each time the function or block is entered; 初始化为任意表达式,甚至是函数调用,每次进入函数时均进行初始化的工作。,C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-39,int binsearch(int x, int v, int n) int low=0; int high=n-1; int mid; . ,Good habit: Generally used explicit assignment, becaus

40、e initializers in declarations are harder to see and further away from the point of use. 尽量用赋值的方式进行初始化,C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-40,Initialization of array,int day =31,28,31,30,31,30,31,31,30,31,30,31;,Declaration with a list of initializer enclos

41、ed in braces and separated by commas. 数组元素对应的数值包括在花括号,依次对应,并用逗号分割。,when the size of array is omitted, the complier will compute the length by counting the initializers; if there are fewer initializers for an array than the number specified, the missing elements will be zero.,int day12=31, 28;,如果只有部分

42、数组元素被赋值,其他元素初始化为0,C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-41,Initialization of char array,char s=“ould”; instead of char s=o, u, l, d, 0;,Note: The size of array is five (four characters plus 0),Back,C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Progra

43、m structure,Page4-42,4.10 Recursion,A function call itself either directly or indirectly.函数直接或间接的调用自身,#include void printd(int n) if(n0) putchar(-); n=-n; if(n/10) printd(n/10); putchar(n%10+0); ,printd 1,printd 2,printd 3,123,1 2 3,12,1,C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program

44、 structure,Page4-43,Quick Sort Algorithm,C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-44,void qsort(int v, int left, int right) int i,last; void swap(int v, int i, int j); if(left=right) /*do nothing if array contains*/ return; /*fewer then two elements*/ swap(v,lef

45、t,(left+right)/2); /*move partition elem to v0*/ last=left; for(i=left+1;i=right;i+)/*partition*/ if(vivleft) swap(v,+last,i); swap(v,left,last);/*restore partition elem*/ qsort(v,left,last-1); qsort(v,last+1,right); ,C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-45,

46、void swap(int v, int i, int j) int temp; temp=vi; vi=vj; vj=temp; ,C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-45,用递归的方法求n!,float fac(int n) float f; if(n0) printf(“n0,data error”); else if (n=0|n=1) f=1; else f=fac(n-1)*n; return(f); main() int n; float y; scanf(“

47、%d”, ,n! =,1 (n=0,1),n*(n-1) ! (n1),Back,C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-46,4.11 The C Preprocessor,C provides certain language facilities by means of a preprocessor, which is conceptually a separate first step in compilation. #include to include the co

48、ntents of a file during compilation #define to replace a token by an arbitrary sequence of characters 用一个字符串替代指定的标示符,C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-47,4.11.1 File Inclusion,#include “filename” #include ,Any source line of following form is replaced by

49、the contents of the file filename,searching for the file begins where the source program was found,searching follows an implementation-defined rule to find the file.,C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-48,4.11.2 Macro Substitution,#define name replacement t

50、ext #define 标识符 语言符号字符串,#define PI 3.1415926 main( ) . area=PI*r*r; .,#define forever for(;) main( ) . forever . .,area=3.1415926*r*r,for(;),设有以下宏定义: #define WIDTH 40; #define LENGTH WIDTH+20; 则执行赋值语句:int v=LENGTH * 40; v的值是_,C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,P

51、age4-48,C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-49,4.11.2 Macro Substitution,#define name(arguments list) replacement text,Macros with arguments,formal arguments,#define max(A,B) (A)(B)? (A) : (B) main() . x=max(p+q,r+s); x=(p+q)(r+s)? (p+q) : (r+s); .,C PROGRA

52、MMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-50,Difference between Macro substitution and Function,for function, actual expression calculated, then send to formal arguments, while for Macro sub just text replacement. for function works during the run time, while Macro sub w

53、orks during the compilation. for function, both function and argument must have type, while Macro sub have no type problem. for function only one value can be returned, while Macro sub can have more return value.,C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-50,Diffe

54、rence between Macro substitution and Function,虽然有参宏与有参函数确实有相似之处,但不同之处更多,主要有以下几个方面: 1)调用有参函数时,是先求出实参的值,然后再复制一份给形参。而展开有参宏时,只是将实参简单地置换形参。 2)使用有参函数,无论调用多少次,都不会使目标程序变长,但每次调用都要占用系统时间进行调用现场保护和现场恢复;而使用有参宏,由于宏展开是在编译时进行的,所以不占运行时间,但是每引用1次,都会使目标程序增大1次。 3)在有参函数中,形参是有类型的,所以要求实参的类型与其一致;而在有参宏中,形参是没有类型信息的,因此用于置换的实参,

55、什么类型都可以。有时,可利用有参宏的这一特性,实现通用函数功能。 4)函数只能返回一个值,利用宏可以返回多个值。,C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-51,4.11.2 Macro Substitution,#define max(A,B) (A)(B)? (A) : (B) . x= max(i+, j+); .,#define square(x) x*x . x=square(z+1); .,z+1*z+1 z+(1*z)+1,#define square(x) (x)*(x),C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-52,4.11.2 Macro Substitution,#define square (x) x*x . x=square(a); .,x= (x) x*x (a),C PROGRAMMING LANGUAGE,E.E.BUAA,CHAPTER 4:Function and Program structure,Page4-53,4.11.2 Macro Substitution,#define PI 3.1415926

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论