变量作用域

变量作用域使用注意案例:

Demo:

#include <stdio.h>

int main(int argc, const char * argv[]) {
    
    int a = 20;
    int score = a + 100;
    printf("1--%d\n",score);//结果:120
    {
        int score = 50;//定义的这个score属于这个作用域的(这个个大括号)
        {
            score = 10;
            printf("2--%d\n",score);//结果:10
        }
        a = 10;//找所定义的a
    }
    {
        score = a + 250;
        printf("3--%d\n",score);//结果:260
        
        int score = 30;//定义的这个score属于这个作用域(大括号)
        printf("4--%d\n",score);//结果:30
    }
    printf("5--%d\n",score);//结果:260
    
    return 0;
}

 

posted @ 2015-12-02 19:37  我就叫M  阅读(137)  评论(0编辑  收藏  举报