INC 161 , CPE 100 Computer Programming

Lecture 10 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. */ 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; // 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 Output: 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(); 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 program_i in main() = 10 int program_i = 10; /* extern int otherfile_i; if otherfile_i file_i in main() = 20 is declared in other file */ program_i in func() = 10 static int file_i = 20; void func(void) { file_i in func() = 20 int function_i = 30; function_i in func() = 30 program_i in block = 10 printf("program_i in func() = %d\n", program_i); printf("file_i in func() = %d\n", file_i); file_i in block = 20 printf("function_i in func() = %d\n", function_i); function_i in block = 30 { int block_i = 40; block_i in block = 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. 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 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 #include static int x = 10; /* declare static variable x */ /* declare extern function func1() */ static int func2(void); /* declare static extern int func1(void); function func2() */ /* declare global variable x; */ int x = 20; int func1(void) { func2(); main() { printf("static 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); } static int func2(void) { printf("static x in func2() = %d\n", x); x++; return 0; }

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 int func(void) { // static variable x is initialized only once static int x=10; // automatic variable y is initialized Output: // each time when func() is called. x = 10 y = 10 int y=10; printf("x = %d y = %d\n", x, y); x = 11 y = 11 x++; y++; x = 11 y = 10 printf("x = %d y = %d\n", x, y); x = 12 y = 11 return 0; } main() { func(); func(); } 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. externfile.c program.c /* File: program.c */ /* File: externfile.c */ #include #include extern int x; int x = 10; /* declare global variable */ extern int func1();

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 #define EXTERNFILE_H #include “externfile.h”

/* 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 file.h #include “externfile.h” main() { include printf("global x in main() = %d\n", x); include x++; func1(); printf("global x in main() = %d\n", x); } file.c main.c