hi guys, I am puzzled by the variable scope problem. I list my questions and answer by myself.
Any other cases and comments are welcome.
How to make a variable available to
1) Only One subroutine?
A
eclare&define it in subroutine.
eg. int Max(){
int len;
}
2) Several subroutines in one source file
3) all subroutines in one source file
A: Define it at the beginning of the source file and use it directly in subroutines.
eg.
int len;
int Max(){
len=1;
}
...
int Min(){
depth = len;
}
//changes of len in Max also changes the value in Min.
4) several subroutines in several source files
A. External declare it in head file. And define and ONLY define it(Is it correct?) in source files where the varialbe is firstly used.
eg. head.h
external int len;
source1.c
int len;
int Max(){
len = 99;
}
source2.c
int Min(){
depth = len(); // here depth is 99 too.
}