/* * HBF */ #include #include "extern.h" int a = 0; // global variable void test1() { auto int b = 0; // local and auto variable static int c = 0; // local and static variable, not auto a += 2; b += 2; c += 2; printf("global a=%d,&a=%lu, auto b=%d,&b=%lu, static c=%d,&c=%lu\n", a, &a, b, &b, c, &c); } void test2() { int b = 0; static int c = 0; a += 2; b += 2; c += 2; printf("global a=%d,&a=%lu, auto b=%d,&b=%lu, static c=%d,&c=%lu\n", a, &a, b, &b, c, &c); } int main() { printf("test register variable\n"); register int j; // register variable for (j=0; j<2; j++) { printf("register variable k=%d, address:%lu\n", j, &k); } printf("\ntest global, auto and static variables\n"); int i; for (i=0; i<2; i++) { test1(); } printf("\ntest global, auto and static variables\n"); for (i=0; i<2; i++) { test2(); } printf("\ntest extern variable\n"); extern int k; printf("extern variable k=%d\n", k); test3(); printf("extern variable k=%d\n", k); printf("\ntest local variable scope\n"); { int d = 2; printf("scope1 d=%d,&d=%lu\n", d, &d); } //printf("scope1 d=%d,&d=\n", d, &d); // out of scope, not allowed by compiler { int d = 3; printf("scope1 d=%d,&d=%lu\n", d, &d); { volatile int e = 1; if (e == 1) { printf("scope d=%d\n", d); printf("scope e=%d\n", e); } } } return 0; } /* test register variable register variable k=0, address:4202496 register variable k=1, address:4202496 test global, auto and static variables global a=2,&a=4214816, auto b=2,&b=6422236, static c=2,&c=4214824 global a=4,&a=4214816, auto b=2,&b=6422236, static c=4,&c=4214824 test global, auto and static variables global a=6,&a=4214816, auto b=2,&b=6422236, static c=2,&c=4214820 global a=8,&a=4214816, auto b=2,&b=6422236, static c=4,&c=4214820 test extern variable extern variable k=5 extern variable k=7 test local variable scope scope1 d=2,&d=6422280 scope1 d=3,&d=6422276 scope d=3 scope e=1 */