我们知道,一个变量的生存周期就是在大括括号内。
int i; for (i = 0; i < 10; i++) { int a = 0; printf("%d %p\n", a++, &a); } printf("%d", a);
上边这段代码,编译时候会报错:
rivsidn@rivsidn:~/demo/C/fragments$ gcc test.c
test.c: In function ‘main’:
test.c:14:15: error: ‘a’ undeclared (first use in this function)
printf("%d", a);
^
test.c:14:15: note: each undeclared identifier is reported only once for each function it appears in
因为,a是在{}中声明的,在中括号外访问不到。
去掉这条打印之后,正常编译运行,结果如下:
rivsidn@rivsidn:~/demo/C/fragments$ ./a.out
0 0xbf98c2c4
0 0xbf98c2c4
0 0xbf98c2c4
0 0xbf98c2c4
0 0xbf98c2c4
0 0xbf98c2c4
0 0xbf98c2c4
0 0xbf98c2c4
0 0xbf98c2c4
0 0xbf98c2c4
地址是一致的,也就是说,在这个循环中,只给 int a分配了一次空间,该空间的地址在这个{}中是不变的。
int i, a = 1; for (i = 0; i < 10; i++) { int a = 0; printf("%d %p\n", a++, &a); } printf("%d\n", a);
改成上边这种之后,能正常编译执行,但是在{}内只能访问到a=0,也就是说在{}内,a=1被覆盖掉了。
中括号内去掉 a=0 这种就不需要赘述了,常见情况。