#include<stdio.h>
void fun_a(void); /* function prototype */
void fun_b(void); /* function prototype */
void fun_c(void); /* function prototype */
int x = 1; /* globe variable */
int main(){
int x = 5; /* local variable to main */
printf("local x in outer scope of main is %d\n", x);
{ /* start new scope */
int x = 7;
printf("local x in inner scope of main is %d\n", x);
} /* end new scope */
printf("local x in outer scope of main is %d\n", x);
fun_a(); /* fun_a has automatic local x */
fun_b(); /* fun_b has static local x */
fun_c(); /* fun_c use globe x */
fun_a(); /* fun_a reinitialize automatic local x */
fun_b(); /* static x retains its previous value */
fun_c(); /* globe x also retain its value */
printf("local x in main is %d\n", x);
return 0;
}
void fun_a(void){
int x = 25; /* initialized each time a is called */
printf("\nlocal x in fun_a is %d after entering fun_a\n", x);
++x;
printf("local x in fun_a is %d before exiting fun_a\n", x);
}
void fun_b(void){
static int x = 50; /* static initialization only */
printf("\nlocal static x is %d on entering fun_b\n", x);
++x;
printf("local static x is %d on exiting fun_b\n", x);
}
void fun_c(void){
printf("\nglobe x is %d on entering fun_c\n", x);
x *= 10;
printf("globe x is %d on exiting fun_c\n", x);
}
globe variable and local variable testing
最新推荐文章于 2021-03-24 16:51:26 发布