STORAGE CLASS.

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.
1) Scope a] Ada scope rules b] C scope rules 2) Parameter passing a] Ada parameter modes b) Parameter passing mechanisms COMP205 IMPERATIVE LANGUAGES 13.
Modular Programming With Functions
Chapter 7: User-Defined Functions II
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 1Winter Quarter Scope of Variables.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
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.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
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.
1 Chapter 9 Scope, Lifetime, and More on Functions.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Variable Scope Storage Class Recursion
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
18. DECLARATIONS.
Learners Support Publications Classes and Objects.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
 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.
Functions g g Data Flow g Scope local global part II g Global Resolution Operator part II.
Week 11 Multi-file Programs and Scope of Variables.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
1 Chapter 9 Scope, Lifetime, and More on Functions.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
1 Scope Lifetime Functions (the Sequel) Chapter 8.
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Dynamic memory allocation and Intraprogram Communication.
Variable Scope. When you declare a variable, that name and value is only “alive” for some parts of the program  We must declare variables before we use.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Constructors and Destructors
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
The Lifetime of a Variable
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.
A Lecture for the c++ Course
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.
Scope, Parameter Passing, Storage Specifiers
Chapter 9 Scope, Lifetime, and More on Functions
Advanced Programming Basics
User Defined Functions
Local Variables, Global Variables and Variable Scope
CSC 253 Lecture 7.
Constructors and Destructors
Classes and Objects.
Storage class.
Scope Rules Of Variables
In C Programming Language
Submitted By : Veenu Saini Lecturer (IT)
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
STORAGE CLASS.
C Programming Lecture-17 Storage Classes
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.
Scope Rules.
Presentation transcript:

STORAGE CLASS

4 Storage classes Automatic variables External Static Register variables

Automatic variables Are declared inside a function in which they are to be utilized. Are created when function is called And destroyed when function is exited. Are private or local to function in which they are declared. Variable declared inside a function without storage class is by default automatic variable.

Main() { Int number; ------- ------ } Auto int number; ---- Automatic variables value cannot be changed accidentally by what happens in some other function in the program. Use same variable name in different functions in the same program without any confusion to compiler.

Main() { int m=1000; function2(); printf(“%d\n”,m); } Function1() int m=10; Printf(“%d\n”,m); Function2() int m=100; Function1(); O/P: 10 100 1000

2 consequences of scope and longevity of auto variables. First any variable local to main will normally live throughout the whole program, although it is active only in main. Main() { int n,a,b; scope level 1 ---- if(n<=100) int n,sum; scope level 2 } -----/* sum not valid here*/ -----

Variables n,a,b defined in main have scope from beginning to end of main. However, variable n defined in main cannot enter into block of scope level 2 bcoz scope level 2 contains another variable named n. Second ‘n’ is available only inside scope level 2 and no lo0nger available the moment control leaves the ‘if’ block. ‘Sum’ defined in ‘if’ block is not available outside that block.

External variables Variable that are both alive and active throughout the entire program. Known as global variables. Can be accessed by any function in the program. Are declared outside a function.

int number; Float length=7 int number; Float length=7.5; Main() { ---- } Function1() Function2() -----

Variables number and length are available for use in all 3 functions. In case a local variable and global variable have same name, the local variable will have precedence over the global one in the function where it is declared. int count; main() { count=10; --- } function() int count=0; -- count=count+1; When the function references variable ‘count’, it will reference only local variable not global one. Value of ‘count’ in main is not affected.

Int x; Main() { x=10; printf(“x=%d\n”,x); printf(“x=%d\n”,fun1()); printf(“x=%d\n”,fun2()); printf(“x=%d\n”,fun3()); } Fun1() x=x+10; return(x);

fun2() { int x; x=1; return(x); } Fun3() x=x+10; Output: x=10 x=20 x=1 x=30 Once variable declared global, any function can use it and cannot change its value. Subsequent functions can refer only that new value.

Main() { y=5; ---- } Int y; Func1() y=y+1; As far main is concerned, ‘y’ is not defined, so compiler issue error message. The statement y=y+1; in func1() will Therefore assign 1 to y.