Storage Classes.

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
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
Storage Classes.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
C++ for Engineers and Scientists Third Edition
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.
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.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
1 Homework / Exam Finish up K&R Chapters 3 & 4 Starting K&R Chapter 5 Next Class HW4 due next class Go over HW3 solutions.
UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
18. DECLARATIONS.
CS 2130 Lecture 5 Storage Classes Scope. C Programming C is not just another programming language C was designed for systems programming like writing.
 2007 Pearson Education, Inc. All rights reserved Random Number Generation  rand function – Load – Returns "random" number between
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.
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.
Senem Kumova Metin // CS115 // FUNCTIONS CHAPTER 5.
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.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Eine By: Avinash Reddy 09/29/2016.
Control Structures (Repetition structure) Jump Statements
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Chapter 7: User-Defined Functions II
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.
LESSON 3 IO, Variables and Operators
A Lecture for the c++ Course
Storage class in C Topics Automatic variables External variables
Chapter 5 Conclusion CIS 61.
trigraph ??= is for # ??( is for [ ??< is for { ??! is for |
Module 5 Sorting and Searching: Bubble sort Selection sort
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Introduction to C Programming Language
Programmazione I a.a. 2017/2018.
Instructor: Ioannis A. Vetsikas
User-Defined Functions
Dynamic memory allocation and Intraprogram Communication
Arrays, For loop While loop Do while loop
Object Oriented Programming COP3330 / CGS5409
Functions: Declaration, Definition, Call and return, Scope of variables, Storage classes, Recursive functions, Recursion vs Iteration.
Govt. Polytechnic,Dhangar
C Storage classes.
Storage class.
Chapter 7: User-Defined Functions II
Homework Finish up K&R Chapters 3 & 4
Scope Rules Of Variables
In C Programming Language
STORAGE CLASS.
Review of Previous Lesson
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Variables in C Topics Naming Variables Declaring Variables
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.
Introduction to Pointers
Presentation transcript:

Storage Classes

To fully define a variable one needs to mention not only its ‘type’ but also its ‘storage class’. In other words, not only do all variables have a data type, they also have a ‘storage class’.

If we don’t specify the storage class of a variable in its declaration, the compiler will assume a storage class depending on the context in which the variable is used. Thus, variables have certain default storage classes.

From C compiler’s point of view, a variable name identifies some physical location within the computer where the string of bits representing the variable’s value is stored. There are basically two kinds of locations in a computer where such a value may be kept— Memory and CPU registers. It is the variable’s storage class that determines in which of these two locations the value is stored.

A variable’s storage class tells us: Where the variable would be stored. What will be the initial value of the variable, if initial value is not specifically assigned.(i.e. the default initial value). (c) What is the scope of the variable; i.e. in which functions the value of the variable would be available. (d) What is the life of the variable; i.e. how long would the variable exist.

There are four storage classes in C:

Automatic Storage Class The features of a variable defined to have an automatic storage class are as under: Storage − Memory. Default initial value − An unpredictable value, which is often called a garbage value. Scope − Local to the block in which the variable is defined. Life − Till the control remains within the block in which the variable is defined

Example: void main( ) { auto int i, j ; printf ( "%d %d", i, j ) ; } Note that the keyword for this storage class is auto Output is Unpredictable....some garbage values will be printed

The output of the above program is: 1 1 1 This is because, all printf( ) statements occur within the outermost block (a block is all statements enclosed within a pair of braces) in which i has been defined. It means the scope of i is local to the block in which it is defined

The moment the control comes out of the block in which the variable is defined, the variable and its value is permanently lost. Consider the following example.

Note that the Compiler treats the three i’s as totally different variables, since they are defined in different blocks or regions.

Register Storage Class The features of a variable defined to be of register storage class are as under: Storage - CPU registers. Default initial value - Garbage value. Scope - Local to the block in which the variable is defined. Life - Till the control remains within the block in which the variable is defined.

A value stored in a CPU register can always be accessed faster than the one that is stored in memory. Therefore, if a variable is used at many places in a program it is better to declare its storage class as register. A good example of frequently used variables is loop counters.

Example void main( ) { register int i ; for ( i = 1 ; i <= 10 ; i++ ) printf ( "\n%d", i ) ; } Keyword is register

Here, even though we have declared the storage class of i as register, we cannot say for sure that the value of i would be stored in a CPU register. Why? Because the number of CPU registers are limited, and they may be busy doing some other task. What happens in such an event... the variable works as if its storage class is auto.

Static Storage Class The features of a variable defined to have a static storage class are as under: Storage − Memory. Default initial value − Zero. Scope − Local to the block in which the variable is defined. Life − Value of the variable persists between different function calls.

Explanation to above example The programs above consist of two functions main( ) and increment( ). The function increment( ) gets called from main( ) thrice. Each time it increments the value of i and prints it. The only difference in the two programs is that one uses an auto storage class for variable i, whereas the other uses static storage class.

Like auto variables, static variables are also local to the block in which they are declared. The difference between them is that static variables don’t disappear when the function is no longer active. Their values persist. If the control comes back to the same function again the static variables have the same values they had last time around.

In the above example, when variable i is auto, each time increment( ) is called it is re- initialized to one. When the function terminates, i vanishes and its new value of 2 is lost. The result: no matter how many times we call increment( ), i is initialized to 1 every time.

On the other hand, if i is static, it is initialized to 1 only once On the other hand, if i is static, it is initialized to 1 only once. It is never initialized again. During the first call to increment( ), i is incremented to 2. Because i is static, this value persists. The next time increment( ) is called, i is not re-initialized to 1; on the contrary its old value 2 is still available. This current value of i (i.e. 2) gets printed and then i = i + 1 adds 1 to i to get a value of 3.

When increment( ) is called the third time, the current value of i (i When increment( ) is called the third time, the current value of i (i.e. 3) gets printed and once again i is incremented. In short, if the storage class is static then the statement static int i = 1 is executed only once, irrespective of how many times the same function is called.

External Storage Class Storage − Memory. Default initial value − Zero. Scope − Global. Life − As long as the program’s execution doesn’t come to an end.

#include<stdio.h> #include<conio.h> int x=21; int main() { extern int y; //external variable declare clrscr(); printf("%d %d",x,y); getch(); return 0; } int y=31; // extern variable is defined #include<stdio.h> #include<conio.h> int x=21; int main() { int y; clrscr(); printf("%d %d",x,y); getch(); return 0; } int y=31;

Conclusion Use static storage class only if you want the value of a variable to persist between different function calls A typical application of register storage class is loop counters, which get used a number of times in a program. Use extern storage class for only those variables that are being used by almost all the functions in the program If you don’t have any of the express needs mentioned above, then use the auto storage class