第8章变量的存储类别;8.1 局部变量和全局变量;8.1.1 局部变量;在一个函数内部定义的变量只在本函数范围内有效
在复合语句内定义的变量只在本复合语句范围内有效
在函数内部或复合语句内部定义的变量称为“局部变量”;float f1( int a)
{ int b,c;
……
}
char f2(int x,int y)
{ int i,j;
……
}
int main( )
{ int m,n;
……
return 0;
};float f1( int a)
{ int b,c;
……
}
char f2(int x,int y)
{ int i,j;
……
}
int main( )
{ int a,b;
……
return 0;
};int main ( )
{ int a,b;
……
{ int c;
c=a+b;
……
}
……
} ;8.1.2 全局变量;int p=1,q=5;
float f1(int a)
{ int b,c; …… }
char c1,c2;
char f2 (int x, int y)
{ int i,j; …… }
int main ( )
{ int m,n;
……
return 0;
};int p=1,q=5;
float f1(int a)
{ int b,c; …… }
char c1,c2;
char f2 (int x, int y)
{ int i,j; …… }
int main ( )
{ int m,n;
……
return 0;
};例1
有一个一维数组,内放10个学生成绩,写一个函数,当主函数调用此函数后,能求出平均分、最高分和最低分。
解题思路
调用一个函数只能得到一个函数返回值,现在希望通过函数调用能得到3个结果。可以利用全局变量来达到此目的。;#include stdio.h
float Max=0,Min=0;
int main()
{ float average(float array[ ],int n);
float ave,score[10]; int i;
printf(Please enter 10 scores:\n);
for(i=0;i10;i++)
scanf(%f,score[i]);
ave=average(score,10);
printf(max=%6.2f\nmin=%6.2f\n
average=%6.2f\n,Max,Min,ave);
return 0;
};float average(float array[ ],int n)
{ int i; float aver,sum=array[0];
Max=Min=array[0];
for(i=1;in;i++)
{ if(array[i]Max) Max=array[i];
else if(array[i]Min) Min=array[i];
sum=sum+array[i];
}
aver=sum/n;
return(aver);
}; ave score 10 Max Min;例2 若外部变量与局部变量同名,分析结果。;#include stdio.h
int a=3,b=5;
int main()
{ int max(int a,int b);
int a=8;
printf(“max=%d\n”,max(a,b));
return 0;
} ?
int max(int a,int b)
{ int c;
c=ab?a:b;
return(c);
};#include stdio.h
int a=3,b=5;
int main()
{ int max(int a,in
您可能关注的文档
- G20峰会知识普及试卷.ppt
- Logistics1101柱塞泵实例试卷.ppt
- 社区卫生服务中心岗位设置与岗位说明书教材.doc
- 技术方案-TBOXRTU数字化油气田监控系统V1.4概览.docx
- 继保自动化考试库概览.docx
- 继电保护二次回路技术问答概览.docx
- CH15整数规划简介试卷.ppt
- 继续教育混凝土概览.docx
- 继续教育推荐8考试真题库,120页概览.docx
- 路灯工程施工方案教材.doc
- 城市轨道交通智能化运维系统在提升乘客体验中的可行性研究报告.docx
- 2025年工业互联网云平台在化妆品行业的应用前景与可行性研究报告.docx
- 云南省2026年面向中南财经政法大学选调优秀毕业生参考题库附答案.docx
- 中化学华谊工程科技集团有限公司2026届校园招聘参考题库附答案.docx
- 2026年佛山市高明区富湾湖实验中学公开招聘临聘教师备考题库及一套答案详解.docx
- 2025年垃圾分类处理技术创新在智能中心建设中的应用场景可行性研究及建议报告.docx
- 2026年云计算在大数据存储与分析中的创新报告.docx
- 2025年环保建材技术创新与建筑行业绿色供应链可行性研究报告.docx
- 2026年佛山市高明区富湾湖实验中学公开招聘临聘教师备考题库及1套完整答案详解.docx
- 2026年佛山市高明区富湾湖实验中学公开招聘临聘教师备考题库及1套参考答案详解.docx
原创力文档

文档评论(0)