局部变量,全局变量的应用

#include<stdio.h>

int count=3;

int main()

    int i,sum,count=2;

    for(i=0,sum=0;i<count;i+=2,count++)

    { 

        static int count=4;

        count++;

        if(i%2==0)

        { 

            extern int count;

            count++;

            sum+=count;

        }

        sum+=count;

    }

    printf("sum=%d\ncount=%d\n",sum,count);

    return 0;

}


    i<count 中 count=2; 进入for循环,static int count=4 中 count 是静态局部变量,使用后保存当前的值,count++ 后,此时 count 变为 5,进入if语句,extern int count,此时 count 为外部的全局变量,count++;此时 count 变为4,执行 sum+=count;sum=4。出if语句,执行 sum+=count, 此时 sum=4+5=9。进入for循环的调整部分,i+=2,count++,此时i=2,count=3;执行for循环的判断部分,2<3成立,进入for循环,继续往下执行,count++,此时count=6,进入if语句,count++,此时count为上次的值加1,所以count为5,sum+=count,则sum=9+5=14;再出if语句,sum+=count,则sum=14+6=20;再进入for循环的调整部分,i+=2,count++,此时i=4,count=4;条件部分4<4判断失败,出for循环,输出sum=20,count=4。程序结束。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值