Download presentation
Presentation is loading. Please wait.
1
Scope Rules Of Variables
The range of code in a program over which a variable has a meaning is called as scope of the variable. Scope of a variable decides as to how it can be accessed by the different parts of the program. 4/12/2019
2
Three types of scopes normally in c: 3. Fill scope or Global scope.
1. Local scope. 2. Function scope. 3. Fill scope or Global scope. 4/12/2019
3
Local Scope A block of statement in C is surrounded by curly braces i.e { and }. We can declare variables inside the block, such variables called as local,can be referenced only within the body of the block. When the control reaches the opening brace ,these variables come into existence and get destroyed as soon as the control leaves the block through the curly brace Click on to see example 4/12/2019
4
Scope of x is the while block. j is the if block.
Click on to see example While(flag) { int x; if(i>x) int j; ……….. …….. ………. } Scope of x is the while block. j is the if block. The variable x can also be referenced to in the if block because this block is completely enclosed in the while block. Scope of j is only in the if block. Formal parameters in function has local scope. 4/12/2019
5
Function Scope The function scope pertains to the labels declared in a function in the sense that a label can be used anywhere in the function in which it is declared. 4/12/2019
6
File scope/Global Scope
Global variables are declared outside all blocks and functions, they are available to all the blocks and functions. The scope of a global variable is in the entire program file. This type of scope is known as file scope. Click to see example of global scope 4/12/2019
7
Why variables possess different types of behavior ?
Variables have different type of behavior. Some variables used in function sub-program locally, but others execute globally. Variables are stored in memory in different ways by using the function. Every time execution period (i.e life time) and boundary(i.e scope and visibility) of the variable vary in function subprograms. Every variable in c language has powerful characteristics called storage class. 4/12/2019
8
Example of global scope
int qty,rate; main() { int gross,net; } fun() { char c; { int a; } } Click to see scope rules Global variabes rate and qty has file scope 4/12/2019
9
The scope rules can be summarized as below
A local variable can be accessed only within the block in which it is declared. The local variable declared in a function exists only during the execution of the function. Therefore these variables are also known as automatic because they automatically created and destroyed. The global variables are not declared in a particular function, they are available to all the functions. Scope of a global variable is in the entire program. 4/12/2019
10
. Life-Time It’s the length of a time a variable store a particular value. The lifetime of a variable is the interval of time in which storage is bound to the variable. 4/12/2019
11
Types of Allocation The action that acquires storage for a variable is called allocation. Some languages allocate storage before run-time. This is called static allocation. Others allocate storage at run-time either using explicit requests (malloc, new) or automatically upon entering a variable’s scope. This is called dynamic allocation. Languages may use both methods. 4/12/2019
12
Identifier reuse: The scope of variables can be global or local.
A global variable’s scope includes all the statements in a program. The scope of a local variable includes only statements inside the function in which it is declared. The same identifier can be reused inside different functions to name different variables. A name is local if it is declared in the current scope, and it is global if declared in an outer scope. 4/12/2019
13
Identifier reuse: A global identifier can be redeclared inside of a function. The new, inner declaration is said to hide the old, outer declaration (what effect does this have?). Some languages provide a scope resolution mechanism for referencing hidden variables. Some languages provide explicit hiding and exporting of variables and functions/methods in order to control visibility (examples?). 4/12/2019
14
Storage Classes Each and every variable declared in C contains not only its data type but also has a storage class specified with it. If we do not specify storage class of a variable compiler will assume its storage class as default i.e automatic. 4/12/2019
15
Storage class of a variable tell us about characteristics of storage classes.
Storage place of the variable i.e memory or CPU registers. The initial value of a variable. The scope or the visibility of a variable The life time of a variable i.e how long the variable exists. 4/12/2019
16
Four types of storage classes in C.
Automatic storage class(with specifier auto). Register storage class(with specifier register). Static storage class(with specifier static). External storage class(with specifier extern). 4/12/2019
17
Automatic storage class(Local variables)
Memory Default initial value Garbage Value i.e unpredictable value Scope or visibility Local or visible to the block in which variable is declared eg.if variable is declared in a function it is only visible to that function. Life Time It retains its value till the control remains in the block in which it is declared. Eg: if the variable is declared in a function, it will retain its value till the control is in that function. Demonstration of auto storage class with default value 4/12/2019
18
Demonstration of auto storage class with default value
main() { auto int i , j=5; int k , l = 10; printf(“\n value of i=%d \n value of j= %d”, i , j); printf(“\n value of k = %d \n value of l=%d”,k,l); } Output: Value of i = 2009 Value of j=5 Value of k=1005 Value of l=10 4/12/2019
19
Demonstration of scope and visibility and lifetime of auto storage class.
main() { auto int x = 5; auto int x = 10; auto int x = 15; { printf(“\n %d”,x); } printf(“%d”,x); } output: 4/12/2019
20
Register Storage Class
. Storage CPU registers Default initial value Garbage Value i.e unpredictable value Scope or visibility Local or visible to the block in which variable is declared eg.if variable is declared in a function it is only visible to that function. Life Time It retains its value till the control remains in the block in which it is declared. Eg: if the variable is declared in a function, it will retain its value till the control is in that function. Demonstration of register storage class 4/12/2019
21
Demonstration of register storage class.
main() { register int i; for(i=1;i<=100;i++) printf(“\n %d”,i); } Speed of the program increases by using a variable as register storage class. 4/12/2019
22
Static Storage Class . Memory zero
Default initial value zero Scope or visibility Local or visible to the block in which variable is declared e.g. if variable is declared in a function it is only visible to that function. Life Time It retains its value between different function calls i.e. when function is called for the first time, static variable is created with initial value zero and in subsequent calls it retains its present value. Demonstration of register storage class 4/12/2019
23
Demonstration of static storage class
main() { int i,r; for(i=1;i<=5;i++) { r=tot (i); printf(“\t %d”,r); } getch(); } tot (int x) static int s=0; s=s+x; return (s); main() calls the function tot() five times, each time sending the value of i varying from 1 to 5.In the function value is added to s and the accumulated result is returned.Once this program is executed following result is returned. output 4/12/2019
24
External Stoarage Calss
Global variables in same file Variables declared in the other file and referred in current file 4/12/2019
25
External variable Memory zero
Storage Memory Default initial value zero Scope or visibility local to the Block in which it is referred Life Time Till the termination of block in which it is referred. Block may be a whole program. 4/12/2019
26
Examples F.c program int ext1, ext 2; sort1( ) { }
//F2.c #incldue “z:\PL\f.c” Extern int ext1; Main() { Ext1=20; printf(“\n\t Ext 1 %d”,Ext1++); 4/12/2019
27
…. Scope and Visibility In identifier's "visibility" determines the portions of the program in which it can be referenced — its "scope." 4/12/2019
28
Euclid’s algorithm in C
int gcd(int m, int n) { int r; while ( (r = m % n) != 0) { m = n; n = r; } return n; Automatic variable Storage allocated on stack when function entered, released when it returns. All parameters, automatic variables accessed w.r.t. frame pointer. Extra storage needed while evaluating large expressions also placed on the stack Excess arguments simply ignored n m Frame pointer ret. addr. Stack pointer r 4/12/2019
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.