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.
Chapter 7: User-Defined Functions II
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.
C++ for Engineers and Scientists Third Edition
1 Chapter 9 Scope, Lifetime, and More on Functions.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
CSCI 130 Scope of Variables Chapter 6. What is scope Parts of program which can access a variable –accessibility –visibility How long variable takes up.
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.
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.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
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.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
 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.
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.
#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.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
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.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
A First Book of ANSI C Fourth Edition
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
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.
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
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.
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Dynamic memory allocation and Intraprogram Communication
Scope, Parameter Passing, Storage Specifiers
Chapter 9 Scope, Lifetime, and More on Functions
Local Variables, Global Variables and Variable Scope
Constructors and Destructors
Classes and Objects.
C Storage classes.
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.
The Three Attributes of an Identifier
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.
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.

Static variable The value of static variable persists until the end of the program. static int x; static float y; Static variable may be either internal or external type depending on place of declaration. Internal static variables are declared inside a function. Similar to auto variables. Internal static variables can be used to retain values between function calls.

main() { int I; for(i=1;i<=3;i++) stat(); } Stat() static int x=0; x=x+1; printf(“x=%d\n”,x); O/P: x=1 x=2 x=3

Static variable is initialized only once, when program is compiled. It is never initialized again. During the first call to stat, x is incremented to 1. Bcoz x is static, this value persists and therefore , the next call adds another 1 to x giving it a value of 2. Value of x becomes 3 when 3rd call is made.

If we declare x as an ‘auto’ variable, the output would be x=1 This is bcoz each time stat is called, the auto variable x is initialized to 0. When the function terminates, its value of 1 is lost.

External static variable is declared outside of all functions and is available to all the functions in that program. Difference b/w static external variable and simple external variable is that static external is available only within the file where it is defined while simple external variable can be accessed by other files.

Register variables Variable should be kept in one of the machines registers, instead of keeping in the memory. Register access is faster than memory access. So frequently accessed variables are kept in registers. register int count; Compilers allow only int or char variables to be placed in register.