C语言程序应用举例

文章作者 100test 发表时间 2007:03:10 17:45:57
来源 100Test.Com百考试题网



这是一个递归函数调用的例子。程序中函数f o r w a r d _ a n d _ b a c k w a r d s ( )的功能是显示一个字符串后反向显示该字符串。
[例4-17] 计算1~7的平方及平方和。
#include
# include
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中被打印。

相关文章


C语言的预处理程序与注释
C语言程序应用举例
计算机二级C语言辅导:指向指针的指针
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛