90 likes | 314 Views
Variable Scope and Lifetime. Recall: Variables declared outside of any blocks are global variables. Variables declared inside of a block are local to that block. A local variable declaration hides the variable defined outside of the block with the same name. int a=1; void test(){
E N D
Recall: Variables declared outside of any blocks are global variables. Variables declared inside of a block are local to that block. A local variable declaration hides the variable defined outside of the block with the same name. int a=1; void test(){ printf("%d",a); } int main( ) { int a=2; { int a=3; printf("%d",a); } printf("%d",a); test(); } Global and Local Variables
You can use a global variable in any part of your program, even in another file. If the real declaration is in another file, you should use extern to let the compiler know. /* test.c */ extern int a; int b=3; int test( ) { printf("a=%d,b=%d\n",a,b); } /* test_main.c */ int a=4; extern int b; int test( ); int main( ) { printf("a=%d,b=%d\n",a,b); a = b = 5; test(); return 0; } External Variables with extern
Auto Variables • Local variables are auto by default. • { int x; …… } is equivalent to • { auto int x; ……} • This means that virtually the variable only exists when the program is in the range of the block. • When the program goes out of the range, and goes into the range again, the variable should be considered as a new variable • The value at the last time is NOT kept. • You have to initialize the variable again.
#include <stdio.h> #include <stdlib.h> void func1(){ int x; print("%d ",x); x = 10; } void func2(){ int y = rand(); } int main(){ int i; for(i=0;i<5;i++){ func1(); func2(); } return 0; } This prints out some random numbers. Means that the value of x is not predictable when next time goes into func1(). Auto Variables Live Locally
Sometimes it is important to keep a local variable long-lived. For example, we want a function to report how many times it has been called. int count( ){ int x=0; return (++x); } The above code would not work since x is auto. We use the static keyword to solve the problem: int count( ){ static int x=0; return (++x); } int main() { printf("%d, %d, %d \n ", count(), count(), count() ); } Static Variables
Auto vs. Static vs. Global • Auto variables only exist when the program is in the block in which the variables are defined; their values do not persist once execution leaves the block. • Static variables exist for the entire time of your program’s running, but can only be referred to when execution is in the block in which they are defined. • Global variables exist all the time, but • Are visible to all of your program unless they are declared static; in which case they are only visible to the file they are declared in. (The same happens if you declare a function to be static!) • Same name means same variable.
Register Variables • Declaring a register variable advises the compiler that a variable is heavily used and should be kept in a machine register to increase speed and efficiency. • For example, a loop counter variable: • register int loop_counter; • A few notes about register variables: • Only automatic variables or formal parameters of a function can be declared as register variables. • Only a few variables in each function may be kept in registers due to limited availability of registers. • Only certain types may be kept in registers. • It is not possible to take the address of a register variable (such as &loop_counter, in the above example). • The compiler is free to ignore your advice. • Most compilers ignore register declarations.