c语言程序设计 谭浩强第8章_函数.ppt

  1. 1、本文档共111页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
c语言程序设计 谭浩强第8章_函数

第八章 8.1 概述 例8.19使用寄存器变量 #include stdio.h void main ( ) { long fac(long); long i,n; scanf(%ld,n); for(i=1;i=n;i++) printf(%ld!=%ld\n,i,fac(i)); } long fac(long n) { register long i,f=1; /*定义寄存器变量*/ for (i=1;i=n;i++) f=f*i; return (f); } 8.9.5 用extern声明外部变量 外部变量是在函数外部定义的全局变量,它的作用域是从变量的定义处开始,到本程序文件的末尾。在此作用域内,全局变量可以为程序中各个函数所引用。编译时将外部变量分配在静态存储区。用extern来声明外部变量,以扩展外部变量的作用城。 1. 在一个文件内声明外部变量 例8.20 用extern声明外部变量,扩展它在程序文件中的作用域 #include stdio.h void main() { int max(int,int); /*外部变量声明*/ extern A,B; printf(%d\n,max(A,B)); } int A=13,B=-8; /*定义外部变量*/ int max(int x,int y) /*定义max函数 */ { int z; z=xy?x:y; return(z); } 2. 在多文件的程序中声明外部变量 #include stdio.h int A; /*定义外部变量*/ void main() { int power(int); /*函数声明*/ int b=3, c, d, m; printf(enter the number a and its power m:\n); scanf(%d,%d,&A, &m); c=A*b; printf ( “%d*%d=%d\n,A,b,c); d=power (m); printf (%d**%d=%d\n,A, m, d); } 例8.21 用extern将外部变量的作用域扩展到其他文件。本程序的作用是给定b的值,输入a和m,求a×b和am的值。文件file1.c中的内容为: #include stdio.h void main() { float average(float array[10]); /* 函数声明 */ float score[10] , aver;   int i;   printf(″input 10 scores:\n″);   for(i=0; i10; i++) scanf(″%f″,&score[i]);   printf(″\n″);   aver=average( score );   printf (″ average score is %5.2f\n″, aver); }  float average (float array[10]) { int i;   float aver,sum=array[0];   for (i=1; i10; i++)   sum=sum+array[i]; aver=sum/10;   return(aver); } 运行情况如下: input 10 scores: 100 56 78 98.5 76 87 99 67.5 75 97↙ average score is 83.40 例 8.12形参数组不定义长度 #include stdio.h void main() { float average(float array[ ], int n); float score_1[5] ={98,5,97,9,1.5,60,5.5}; float score_2[10]= { 67.5,89.5,99,69.5,77, 89.5,76.5,54,60,99.5};  

文档评论(0)

yaocen + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档