globe variable and local variable testing

#include<stdio.h>

void fun_a(void);    /* function prototype */
void fun_b(void);    /* function prototype */
void fun_c(void);    /* function prototype */

int x = 1;            /* globe variable */

int main(){
    int x = 5;    /* local variable to main */
    printf("local x in outer scope of main is %d\n", x);

    {    /* start new scope */
        int x = 7;
        printf("local x in inner scope of main is %d\n", x);
    }    /* end new scope */

    printf("local x in outer scope of main is %d\n", x);

    fun_a();    /* fun_a has automatic local x */
    fun_b();    /* fun_b has static local x */
    fun_c();    /* fun_c use globe x */
    fun_a();    /* fun_a reinitialize automatic local x */
    fun_b();    /* static x retains its previous value */
    fun_c();    /* globe x also retain its value */

    printf("local x in main is %d\n", x);
    return 0;
}

void fun_a(void){
    int x = 25;    /* initialized each time a is called */
    printf("\nlocal x in fun_a is %d after entering fun_a\n", x);
    ++x;
    printf("local x in fun_a is %d before exiting fun_a\n", x);
}

void fun_b(void){
    static int x = 50; /* static initialization only */
    printf("\nlocal static x is %d on entering fun_b\n", x);
    ++x;
    printf("local static x is %d on exiting fun_b\n", x);
}

void fun_c(void){
    printf("\nglobe x is %d on entering fun_c\n", x);
    x *= 10;
    printf("globe x is %d on exiting fun_c\n", x);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值