c语言中块作用域的优先级高于文件作用域

 

001、

[root@PC1 test]# ls
test.c
[root@PC1 test]# cat test.c
#include <stdio.h>

int a = 100;         // 该变量具有文件作用域

int main(void)
{
        printf("a = %d\n", a);     // 在程序快中调用外部变量

        return 0;
}
[root@PC1 test]# gcc test.c -o kkk
[root@PC1 test]# ls
kkk  test.c
[root@PC1 test]# ./kkk
a = 100

 。

 

002、

[root@PC1 test]# ls
test.c
[root@PC1 test]# cat test.c          ## 测试程序
#include <stdio.h>

int a = 100;

int main(void)
{
        int a = 500;        // 块作用域的变量, 优先级高于文件作用域
        printf("a = %d\n", a);

        return 0;
}
[root@PC1 test]# gcc test.c -o kkk      ## 编译
[root@PC1 test]# ls
kkk  test.c
[root@PC1 test]# ./kkk                  ## 运算测试
a = 500

 。

 

posted @ 2024-11-22 13:03  小鲨鱼2018  阅读(10)  评论(0)    收藏  举报