静态局部变量的累积效应

/*
*copyright(c) 2018,HH
*All rights reserved.
*作 者:HH
*完成日期:2018年7月10日
*版本号:v1.0
*
*问题描:;体验静态局部变量的累积效应
*输入描述:;
*程序输出:;。
*/
#include <stdio.h>
int f(int a)//自定义函数的定义
{
    auto int b=0;//局部变量b每次使用后都被释放(变成随机数),每次使用前重新赋值0;
    static int c=3;//定义c为“静态局部变量”,整个程序结束前c不会被释放,c的值会累积直至整个程序结束!!!
    b=b+1;//每次都是0+1=1
    c=c+1;//累积值+1
    return a+b+c;//a虽然属于主函数中的局部变量,但可以传递至被调函数f(int a)中f(2)
}
int main( )
{
    int a=2,i;
    for(i=0; i<3; i++)
    {
        printf("%d ", f(a));//a的值始终为2,并传递至被调函数f(int a)中f(2)!
        printf("\n");
    }
    return 0;
}

/*
定义1
功能:
参数:
返回值:
算法:
*/

/*
定义2
功能:
参数:
返回值:
算法:
*/



猜你喜欢

转载自blog.csdn.net/pl0020/article/details/80986713