1 / 28

Scope Rules Of Variables

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. Local Scope.

paubry
Download Presentation

Scope Rules Of Variables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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.

  2. .

  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. 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.

  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.

  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

  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.elife time) and boundary(i.escope and visibility) of the variable vary in function subprograms. • Every variable in c language has powerful characteristics called storage class.

  8. Example of global scope Global variabes rate and qty has file scope intqty,rate;main(){intgross,net; ------------------ -----------------}fun(){ char c; ----------- {int a; ----- ----- } }Click to see scope rules

  9. The scope rules can be summarized as below

  10. .

  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.

  12. Identifier reuse: • The scope of variables can be global or local. • A global variable’sscope includes all the statements in a program. • The scope of a local variableincludes only statements inside the function in which it is declared. • Thesame 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.

  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?).

  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.eautomatic.

  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.

  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).

  17. Automatic storage class(Local variables)

  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

  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); } printf(“%d”,x); } }output: 15 10 5

  20. Register Storage Class .

  21. Demonstration of register storage class. main() { register inti; for(i=1;i<=100;i++) { printf(“\n %d”,i); } } Speed of the program increases by using a variable as register storage class.

  22. Static Storage Class .

  23. Demonstration of static storage class 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 1 3 6 10 15 main() { inti,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); }

  24. External StoarageCalss • Global variables in same file • Variables declared in the other file and referred in current file

  25. External variable

  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++);

  27. ….

  28. Euclid’s algorithm in C intgcd(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

More Related