#include <stdio.h> int x = 77; void fn1() { printf("fn1(): x=%d\n", x); } int main() { int x = 10; if(x>0) { int x = 100; x /= 2; printf("if语句内, x=%d\n", x); } printf("main方法内, x=%d\n", x); fn1(); return 0; }
得到结果:
if语句内, x=50
main方法内, x=10
fn1(): x=77
#include <stdio.h> x = 77; void fn1() { printf("fn1(): x=%d\n", x); } int main() { int x = 10; if(x>0) { int x = 100; x /= 2; printf("if语句内, x=%d\n", x); } printf("main方法内, x=%d\n", x); fn1(); return 0; }
得到结果:
if语句内, x=50
main方法内, x=10
fn1(): x=77
#include <stdio.h> x = 77; void fn1() { printf("fn1(): x=%d\n", x); } int main() { x = 10; if(x>0) { int x = 100; x /= 2; printf("if语句内, x=%d\n", x); } printf("main方法内, x=%d\n", x); fn1(); return 0; }
得到结果:
if语句内, x=50
main方法内, x=10
fn1(): x=10
#include <stdio.h> x = 77; void fn1() { printf("fn1(): x=%d\n", x); } int main() { x = 10; if(x>0) { x = 100; x /= 2; printf("if语句内, x=%d\n", x); } printf("main方法内, x=%d\n", x); fn1(); return 0; }
得到结果:
if语句内, x=50
main方法内, x=50
fn1(): x=50
各位对于这些对比有什么想法?int对于局部全局的影响是什么?
顺带捎一个自己不确定的问题:局部变量的优先级高于全局变量对吗?但是不定义的话会默认为全局变量的值这样吗?
拿这题来说,全局变量是X的最初值,如果不定义的话直接就是最初值了。。(这是我的理解)
sad