220 likes | 236 Views
Learn about the scope of variables in computer programming, including global variables shared between functions and local variables within functions. Understand the different memory locations where variable values are stored.
E N D
INC 161 , CPE 100Computer Programming Lecture 10 Scope of Variables
Global and Local Variables Although it is not recommended, global variables shared by different functions can be used to communicate between functions. The same identifiers can be used in different scopes. The values of the variables are stored in different memory locations.
Example global Output: Global variable /* File: global.c */ #include <stdio.h> int g = 10; // global variable void func1(void) { g++; printf("g in func1() = %d\n", g); } void func2(void) { g++; printf("g in func2() = %d\n", g); } void func3(void) { int g=0; // local variable g++; printf("g in func3() = %d\n", g); } main() { func1(); func2(); func3(); printf("g in main() = %d\n", g); } g in func1() = 11 g in func2() = 12 g in func3() = 1 g in main() = 12 g is the same is all functions because it is a global variable
Example local Output: /* File: local.c */ #include <stdio.h> void func1(void) { int g = 1; //try removing this line later g++; printf("g in func1() = %d\n", g); } main() { int g = 1; printf("g in main before func1() = %d\n", g); func1(); printf("g in main after func1() = %d\n", g); } g is different when it is in different {..} pair g in main before func1() = 1 g in func1() = 2 g in main after func1() = 1
Scope of Identifiers The scope of an identifier is the portion of the program in which the identifier can be accessed. There are four types of scope: program scope, file scope, function scope, and block scope. • Program scope. The identifiers having a program scope are accessible among different files. Variables with program scope are called global variables. • File scope. The identifiers having a file scope are active from its declaration point to to the end of the file. The global static variables have a file scope. • Function scope. The identifiers having a function scope are active from the beginning to the end of the function. The variables declared at the beginning of a function have a function scope. • Block scope. A block is a bunch of statements enclosed in braces. The identifiers having a block scope is active from its declaration point to the end of the block in which it is declared. The variables declared inside a block have a block scope.
Scope of Identifiers Program Scope File Scope Function Scope Block Scope Function Scope Block Scope
Example: Output: program_i in main() = 10 file_i in main() = 20 program_i in func() = 10 file_i in func() = 20 function_i in func() = 30 program_i in block = 10 file_i in block = 20 function_i in block = 30 block_i in block = 40 /* File: scopeid.c */ #include <stdio.h> int program_i = 10; /* extern int otherfile_i; if otherfile_i is declared in other file */ static int file_i = 20; void func(void) { int function_i = 30; printf("program_i in func() = %d\n", program_i); printf("file_i in func() = %d\n", file_i); printf("function_i in func() = %d\n", function_i); { int block_i = 40; printf("program_i in block = %d\n", program_i); printf("file_i in block = %d\n", file_i); printf("function_i in block = %d\n", function_i); printf("block_i in block = %d\n", block_i); } } main() { printf("program_i in main() = %d\n", program_i); printf("file_i in main() = %d\n", file_i); func(); }
Indent Style To make the scope easier to see, programmers should have a system to leave indent space. This is like a handwriting style. Here are some popular styles.
Allman style Note: You can comment the while line without error
Horstmann Style Note: Combine Allman style and save lines
Storage Class C provides four storage classes indicated by the storage-class specifiers. The valid storage-class specifiers are given in the following table. (normal)
auto or register: Keyword auto or register is used to declare variables of automatic storage duration. The automatic storage duration is legal only for variables with block scope. Since this is the default storage type, the keyword auto or register is rarely used. static: Keyword static is used to declare variables of static storage duration inside or outside a function. For such a variable, storage is reserved and its stored value is initialized only once. The variable exists, has constant address, and retains its last-stored value throughout the execution of the entire program. When the variable is declared inside a function, the keyword static makes a variable has static storage duration instead of the default automatic duration. For a variable declared outside a function, the keyword static gives the variable file scope instead of program scope. extern: The keyword extern is used to declare global variables which are defined either later in the same file or in a different file.
Static Variables Outside Function A variable declared outside function by the keyword static has file scope instead of program scope. In the following example, the static variable x and static function func2() can only be accessed inside the file staticfile.c.
/* File: staticfile.c */ #include <stdio.h> static int x = 10; /* declare static variable x */ static int func2(void); /* declare static function func2() */ int func1(void) { func2(); printf("static x in func1() = %d\n", x); x++; return 0; } static int func2(void) { printf("static x in func2() = %d\n", x); x++; return 0; } /* File: staticprog.c */ #include <stdio.h> /* declare extern function func1() */ extern int func1(void); /* declare global variable x; */ int x = 20; main() { printf("global x in main() = %d\n", x); x++; func1(); printf("global x in main() = %d\n", x); } staticprog.c staticfile.c global x in main() = 20 static x in func2() = 10 static x in func1() = 11 global x in main() = 21 Output of staticprog.c
Static Variables Inside a Function Local variables declared inside a function with the keyword static have static duration.The difference between an automatic variable and a static local variable is that the latter retains its value even when the function is exited. When the function is called next time, the static local variable contains the value it had when the function exited last time. The following example illustrates this difference.
Example: /* File: staticf.c */ #include <stdio.h> int func(void) { // static variable x is initialized only once static int x=10; // automatic variable y is initialized // each time when func() is called. int y=10; printf("x = %d y = %d\n", x, y); x++; y++; printf("x = %d y = %d\n", x, y); return 0; } main() { func(); func(); } Output: x = 10 y = 10 x = 11 y = 11 x = 11 y = 10 x = 12 y = 11
External Variables and Functions The following example demonstrates how to use extern to declare external variables and functions. The global variable x and function func1() are defined in the file externfile.c. In order to access them in the file externprog.c, the keyword extern must be used to declare the external variable x and function func1() in the file externprog.h.
/* File: externfile.c */ #include <stdio.h> int x = 10; /* declare global variable */ int func1(void) { printf("global x in func1() = %d\n", x); x++; return 0; } /* File: program.c */ #include <stdio.h> extern int x; extern int func1(); main() { printf("global x in main() = %d\n", x); x++; func1(); printf("global x in main() = %d\n", x); } program.c externfile.c Compile with gcc externfile.c program.c Output of externprog.c global x in main() = 10 global x in func1() = 11 global x in main() = 12
/* File: externfile.c */ #include <stdio.h> #include “externfile.h” int x = 10; /* declare global variable */ int func1(void) { printf("global x in func1() = %d\n", x); x++; return 0; } /* File: program.c */ #include <stdio.h> #include “externfile.h” main() { printf("global x in main() = %d\n", x); x++; func1(); printf("global x in main() = %d\n", x); } Example: Interface with .h file externfile.h externfile.c /* File: externfile.h */ #ifndef EXTERNFILE_H #define EXTERNFILE_H /* declare extern variable x */ extern int x; /* declare extern function func1(void) */ extern int func1(); #endif program.c file.h include include file.c main.c