教程学院
图像设计 多媒体类 机械制图 办公软件 操作系统 系统编程 网站编程 网页制作 数据库类 网络路由 网络工程 网络安全 考试认证
firefox火狐浏览器下载
酷网学院
CAD
AutoCad Cam350 ProEngineer GCcam MATLAB Unigraphics SolidWorks CAXA Solid3000 Cimatron EdgeCAM
系统
安全 防火墙 病毒 WinXP Win2003 Vista
数据库
编程
网络
精彩图库
  当前位置: 库库中文网 · 系统编程教程 · C语言 · C语言基础教程

C语言程序运用举例

学院最新推荐文章
教程推荐
『C语言程序运用举例』如果文章有大量图片,显示会较慢,请等待图片下载完成
 
点击数: 更新时间:2005-6-7 



这是一个递归函数调用的例子。程序中函数f o r w a r d _ a n d _ b a c k w a r d s ( )的功能是显示一个字符串后反向显示该字符串。
[例4-17] 计算1~7的平方及平方和。
#include <stdio.h>
# include<math.h>
void header(); / *函数声明* /
void square(int number);
void ending();
int sum; /* 全局变量* /
m a i n ( )
{
int index;
h e a d e r ( ) ; / *函数调用* /
for (index = 1;index <= 7;i n d e x + + )
s q u a r e ( i n d e x ) ;
e n d i n g ( ) ; / *结束* /
}
void header()
{
sum = 0; /* 初始化变量"sum" */
printf("This is the header for the square program\n;\n")
}
void square(int number)
{
int numsq;
numsq = number * numbe;r
sum += numsq;
printf("The square of %d is %d\,nn"u m b e r ,nu m s q ) ;
}
void ending()
{
printf("\nThe sum of the squares is %d,\ns"u m ) ;
}
运行程序:
R U N ¿
This is the header for the square program
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25
The square of 6 is 36
The square of 7 is 49
The sum of the squares is 140
这个程序打印出1到7的平方值,最后打印出1到7的平方值的和,其中全局变量s u m在多个
函数中出现过。
全局变量在h e a d e r中被初始化为零;在函数s q u a r e中,s u m对n u m b e r的平方值进行累加,也就是说,每调用一次函数s q u a r e和s u m就对n u m b e r的平方值累加一次;全局变量s u m在函数
e n d i n g中被打印。
[例4-18] 全局变量与局部变量的作用。
#include <stdio.h>
void head1(void);
void head2(void);
void head3(void);
int count; /* 全局变量* /
m a i n ( )
{
register int index; / *定义为主函数寄存器变量* /
h e a d 1 ( ) ;
h e a d 2 ( ) ;
h e a d 3 ( ) ;
for (index = 8;index > 0;index--) /* 主函数"for" 循环* /
{
int stuff; /* 局部变量* /
/* 这种变量的定义方法在Turbo C 中是不允许的* /
/* stuff 的可见范围只在当前循环体内* /
for(stuff = 0;stuff <= 6;s t u f f + + )
printf("%d ",s t u f f ) ;
printf(" index is now %d\,n"in d e x ) ;
}
}
int counter; /* 全局变量* /
/* 可见范围为从定义之处到源程序结尾* /
void head1(void)
{
int index; / * 此变量只用于head1 */
index = 23;
printf("The header1 value is %d\,n"in d e x ) ;
}
void head2(void)
{
int count; /* 此变量是函数

[1] [2] 下一页  

作者:无 来源:无
】【关闭窗口
·上页:
·下页:
相关文章
     系统编程教程 - C语言基础教程
普通教程新手必看
普通教程新手必看
普通教程趣味程序导学C语言
普通教程用Win-TC编译与用TC2.0编译地差
普通教程C语言学习初次体会
普通教程C语言漫谈
普通教程Linux下面怎么样进行C语言编制程
普通教程怎么样在一个程序后面运行另一个
普通教程新手问与答(怎么样产生random数
普通教程C语言程序设计(第9章 实用编制程
普通教程C语言程序设计(第9章 实用编制程
普通教程C语言程序设计(第8章 输入输出和
精彩图片汇集
advertisement
关于站点 - 广告服务 - 联系我们 - 版权隐私 - 免责声明 - 合作伙伴 - 程序支持 - 网站地图 - 返回顶部
网站文本地图
版权所有:库库中文 2005-2007 欢迎各种媒体转载我们的原创作品[转载请注明出处]
copyright © 2005-2008 www.QQGB.com online services. all rights reserved. 蜀ICP备05015578
Template designed by Virus. Optimized for 1024x768 to Firefox,Opera and MS-IE6. Site powered by EQL.
红盾
热爱电脑,热爱生活
拥有电脑,拥有生命
让我们享受拥有电脑的时光