INC 161 , CPE 100 Computer 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. /* File: global.c */ Example global #include
int g = 10; // global variable Output: void func1(void) { Global variable g++; printf("g in func1() = %d\n", g); } g in func1() = 11 g in func2() = 12 void func2(void) { g in func3() = 1 g++; printf("g in func2() = %d\n", g); g in main() = 12 }
void func3(void) { int g=0; // local variable g is the same g++; printf("g in func3() = %d\n", g); is all functions }
because it is a main() { global variable func1(); func2(); func3(); printf("g in main() = %d\n", g); } Example local /* File: local.c */ #include
main() { int g = 1; printf("g in main before func1() = %d\n", g); func1(); g is different printf("g in main after func1() = %d\n", g); 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: /* File: scopeid.c */ #include
} 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. K&R Style Allman style
Note: You can comment the while line without error Whitesmiths Style GNU Style 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.
Specifier Function auto or register local automatic variable (normal) static static variable extern external variable 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. staticfile.c staticprog.c /* File: staticfile.c */ /* File: staticprog.c */ #include
Output of staticprog.c global x in main() = 20 static x in func2() = 10 static x in func1() = 11 global x in main() = 21 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
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. externfile.c program.c /* File: program.c */ /* File: externfile.c */ #include
int func1(void) { main() { printf("global x in func1() = %d\n", x); printf("global x in main() = %d\n", x); x++; x++; return 0; func1(); } printf("global x in main() = %d\n", x); }
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 Example: Interface with .h file externfile.h externfile.c
/* File: externfile.h */ /* File: externfile.c */ #ifndef EXTERNFILE_H #include
/* declare extern variable x */ int x = 10; /* declare global variable */ extern int x; int func1(void) { /* declare extern function func1(void) */ printf("global x in func1() = %d\n", x); extern int func1(); x++; return 0; } #endif program.c
/* File: program.c */ #include