Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.

Similar presentations


Presentation on theme: "1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material."— Presentation transcript:

1 1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material

2 2 Each variable in a C program has 2 properties Storage Duration –Is the portion of the program execution during which storage for the variable exists –Can be temporary (automatic) or permanent Scope –Is the portion of the code in which the variable can be referenced –Can be block scope or file scope Storage Duration & Scope

3 3 Local and Global Variables Broadly speaking all variables in C can be classified as follows: Local –Only meaningful inside the function or the code block (compound statement) that declares them –Block scope Global –Their declaration is outside of any function –Can be used by all functions in a program –Permanent storage and File scope –This goes against modularity –Avoid using them!

4 4 Local and Global Variables #include int global; /* global var: permanent storage, file scope */ void print_it(void){ int local = 3; /* local var: auto storage, block scope */ printf("global = %d, local: %d\n", global, local); } /* end-print_it */ int main(void){ int local = 5; /* temporary (auto) storage, block scope */ global = local * local; print_it(); return 0; } /* end-main */ global = 25, local: 3

5 5 Local&Global Vars: Another Example #include int sum; /* global var: permanent storage, file scope */ void Sum(int n){ /* parameter n: auto storage, block scope */ int i; /* local var: auto storage, block scope */ sum = 0; /* Access a global var */ for (i=1; i<=n; i++) sum +=i; } /* end-Sum */ void main(void){ int sum1; /* local var: auto storage, block scope */ int sum2; /* local var: auto storage, block scope */ Sum(10); sum1 = sum; /* Access a local var + global var */ Sum(20); sum2 = sum; /* Access a local var + global var */ printf(sum1: %d, sum2: %d\n, sum1, sum2); } /* end-main */

6 6 Automatic Variables #include int factorial(auto int n); int main() { auto int n = 5; printf("n! = %d\n", factorial(n)); printf("2! = %d\n", factorial(2)); } int factorial(auto int n) { auto int prod = 1; for( ; n > 1; n--) prod *= i ; return prod; } n! = 120 2! = 2 Declared within a function and are local to the function in which they are declared, that is, the scope of the variable is confined to that function Do not retain their value once the control is transferred out of its defining function (any value assigned to an automatic variable within a function will be lost once the function is exited) auto keyword may be omitted

7 7 Static Variables #include /* This is a buggy factorial function */ int factorial(int n) { static int prod = 1; for( ; n > 1; n--) prod *= n; return prod; } int main() { int n = 5; printf("n! = %d\n", factorial(n)); printf("2! = %d\n", factorial(2)); } n! = 120 2! = 240 Static variables are like global variables except that they are only accessible in the code block where they are declared Static variables retain their value between calls

8 8 External Global Variables If the program code is spread over multiple files, you may want to refer to a global variable declared in another file –To do this, you declare the variable extern in the file where you want to refer to it a.c int x; /* Global var */ void main(void){ extern void f(void); f(); printf(x: %d\n, x); } b.c extern int x; /* external global var * means that int x is declared * in some other file, and I want * to refer to it here */ void f(void){ x = 2; }

9 9 Register Variables register keyword is used to make code more efficient Results are compiler-dependent register variables use CPU registers (rather than RAM memory) whenever compiler can Good compilers do this anyway register int sum = 0; register int i; for (i=1; i <= 100; i++) sum += i;


Download ppt "1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material."

Similar presentations


Ads by Google