当前位置:首页 > 网络编程 > 软件语言 > C语言 > C语言程序应用举例

C语言程序应用举例

点击次数:24 次 发布日期:2008-12-01 11:58:36 作者:源代码网
源代码网推荐
源代码网推荐这是一个递归函数调用的例子。程序中函数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; /* 此变量是函数h e a d 2 ( ) 的局部变量* /
源代码网推荐/* 此变量名与全局变量c o u n t 重名* /
源代码网推荐/* 故全局变量c o u n t 不能在函数h e a d 2 ( ) 中使用* /
源代码网推荐count = 53;
源代码网推荐printf("the header2 value is %d\,n"co u n t ) ;
源代码网推荐counter = 77;
源代码网推荐}
源代码网推荐void head3(void)
源代码网推荐{
源代码网推荐printf("the header3 value is %d\,nc"o u n t e r ) ;
源代码网推荐}
源代码网推荐运行程序:
源代码网推荐r u n ¿
源代码网推荐the headerl value is 23
源代码网推荐the header2 value is 53
源代码网推荐the header3 value is 77
源代码网推荐0 1 2 3 4 5 6 index is now 8
源代码网推荐0 1 2 3 4 5 6 index is now 7
源代码网推荐0 1 2 3 4 5 6 index is now 6
源代码网推荐0 1 2 3 4 5 6 index is now 5
源代码网推荐0 1 2 3 4 5 6 index is now 4
源代码网推荐0 1 2 3 4 5 6 index is now 3
源代码网推荐0 1 2 3 4 5 6 index is now 2
源代码网推荐0 1 2 3 4 5 6 index is now 1
源代码网推荐该程序的演示帮助读者来了解全局变量、局部变量的作用域,请仔细理解体会。
源代码网推荐 源代码网供稿.
上一篇: 一维数组  下一篇: 指针与指针变量
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华