精彩百例:局部和全局变量

/*
    filename:local variable and global variable
    function:introduce the difference of the local variable and the local variable
*/
# include <stdio.h>

int count;
void func1();
void func2();
int main(void)
{
    count = 100;
    func1();

    return 0;
}
void func1()
{
    int temp;
    temp = count;
    func2();
    printf(" count is %d\n", count);
    func2();
}
void func2()
{
    int count;//define a local variable.when the local variable is the same as global variable,
              //the function use the local variable,not the global variable
    for(count=1; count<20; count++)
    {
        printf(".");
    }
    printf("\n");
}
/*
    在函数内部定义的变量,仅在函数内部有效,称为内部变量或者局部变量
    在函数外部定义的变量,是全局变量,可以被文件域内其他函数使用
    它的有效范围是从定义变量的位置开始到源文件结尾
*/

result:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值