Scope Rules Of Variables

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Storage class in C Topics Automatic variables External variables
1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Functions and scope Confidence In scope Reference: K&K textbook, Chapter 4.
Chapter 7: User-Defined Functions II
1 Review of Class on Oct Outline  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
Storage Classes.
C++ for Engineers and Scientists Third Edition
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
18. DECLARATIONS.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
 2007 Pearson Education, Inc. All rights reserved Random Number Generation  rand function – Load – Returns "random" number between
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
Dynamic memory allocation and Intraprogram Communication.
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
Advanced Programming in C
Eine By: Avinash Reddy 09/29/2016.
Advanced Programming with C
Functions and an Introduction to Recursion
C Functions -Continue…-.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
The Three Attributes of an Identifier
Storage class in C Topics Automatic variables External variables
Object Lifetime and Dynamic Objects
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Instructor: Ioannis A. Vetsikas
This pointer, Dynamic memory allocation, Constructors and Destructor
C++ for Engineers and Scientists Second Edition
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
Scope, Visibility, and Lifetime
Classes and Objects.
C Storage classes.
Storage class.
Scope Rules and Storage Types
UNIT V Run Time Environments.
In C Programming Language
The vector Container Type
STORAGE CLASS.
STORAGE CLASS.
The Three Attributes of an Identifier
C Programming Lecture-17 Storage Classes
More C++ Classes Systems Programming.
Storage classes in C In C language, each variable has a storage class which decides the following things: scope i.e where the value of the variable would.
Storage Classes.
Presentation transcript:

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

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

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

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

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

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

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

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

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

. 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

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

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

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

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

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

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

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

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

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: 15 10 5 4/12/2019

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

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

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

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 1 3 6 10 15 4/12/2019

External Stoarage Calss Global variables in same file Variables declared in the other file and referred in current file 4/12/2019

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

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

…. Scope and Visibility In identifier's "visibility" determines the portions of the program in which it can be referenced — its "scope."  4/12/2019

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