Storage class in C Topics Automatic variables External variables

Slides:



Advertisements
Similar presentations
Storage class in C Topics Automatic variables External variables
Advertisements

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Programming In C++ Spring Semester 2013 Lecture 12 Programming In C++, Lecture 12 By Umer Rana.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition 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.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Scope Rules and Storage Types CS-2303, C-Term Scope Rules and Storage Types CS-2303, System Programming Concepts (Slides include materials from The.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
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.
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
Storage Classes.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Modules and Scope Gabriel Hugh Elkaim Spring 2013.
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.
Objects Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
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.
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.
CHAPTER 8 Scope, Lifetime, and More on Functions.
Revisiting building. Preprocessing + Compiling 2 Creates an object file for each code file (.c ->.o) Each.o file contains code of the functions and structs.
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.
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.
Advanced Programming in C
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.
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
A Lecture for the c++ Course
Chapter 5 Conclusion CIS 61.
Object Lifetime and Dynamic Objects
trigraph ??= is for # ??( is for [ ??< is for { ??! is for |
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
Classes and Objects.
C Storage classes.
Storage class.
Scope Rules and Storage Types
Chapter 7: User-Defined Functions II
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.
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 in C Topics Automatic variables External variables Static variables Register variables Scopes and longevity of above types of variables.

Few terms Scope: the scope of a variable determines over what part(s) of the program a variable is actually available for use(active). Longevity: it refers to the period during which a variables retains a given value during execution of a program(alive) Local(internal) variables: are those which are declared within a particular function. Global(external) variables: are those which are declared outside any function.

Automatic variables Are declare inside a function in which they are to be utilized. Are declared using a keyword auto. eg. auto int number; Are created when the function is called and destroyed automatically when the function is exited. This variable are therefore private(local) to the function in which they are declared. Variables declared inside a function without storage class specification is, by default, an automatic variable.

Example program int main() { int m=1000; function2(); printf(“%d\n”,m); } function1() { int m = 10; function2() { int m = 100; function1(); Output 10 100 1000

Few observation abt auto variables Any variable local to main will normally live throughout the whole program, although it is active only in main. During recursion, the nested variables are unique auto variables. Automatic variables can also be defined within blocks. In that case they are meaningful only inside the blocks where they are declared. If automatic variables are not initialized they will contain garbage.

External Variables These variables are declared outside any function. These variables are active and alive throughout the entire program. Also known as global variables and default value is zero. Unlike local variables they are accessed by any function in the program. In case local variable and global variable have the same name, the local variable will have precedence over the global one. Sometimes the keyword extern used to declare these variable. It is visible only from the point of declaration to the end of the program.

External variable (examples) int number; float length=7.5; main() { . . . . . . } funtion1() {. . . int count; main() {count=10; . . . } funtion() {int count=0; count=count+1; When the function references the variable count, it will be referencing only its local variable, not the global one. The variable number and length are available for use in all three function

Global variable example int x; int main() { x=10; printf(“x=%d\n”,x); printf(“x=%d\n”,fun1()); printf(“x=%d\n”,fun2()); printf(“x=%d\n”,fun3()); } int fun1() { x=x+10; return(x); int fun2() { int x x=1; int fun3() { x=x+10; return(x); } Once a variable has been declared global any function can use it and change its value. The subsequent functions can then reference only that new value. Output x=10 x=20 x=1 x=30

External declaration int main() { y=5; . . . } int y; func1() y=y+1 As far as main is concerned, y is not defined. So compiler will issue an error message. There are two way out at this point Define y before main. Declare y with the storage class extern in main before using it.

External declaration(examples) int main() { extern int y; . . . } func1() int y; Note that extern declaration does not allocate storage space for variables

Multifile Programs and extern variables file2.c file1.c int main() { extern int m; int i . . . } function1() int j; int m; function2() { int i . . . } function3() int count;

Multifile Programs and extern variables file2.c file1.c int m; int main() { int i; . . . } function1() int j; extern int m; function2() { int i . . . } function3() int count;

Static Variables The value of static variables persists until the end of the program. It is declared using the keyword static like static int x; static float y; It may be of external or internal type depending on the place of there declaration. Static variables are initialized only once, when the program is compiled.

Internal static variable Are those which are declared inside a function. Scope of Internal static variables extend upto the end of the program in which they are defined. Internal static variables are almost same as auto variable except they remain in existence (alive) throughout the remainder of the program. Internal static variables can be used to retain values between function calls.

Examples (internal static) Internal static variable can be used to count the number of calls made to function. eg. int main() { int I; for(i =1; i<=3; i++) stat(); } void stat() static int x=0; x = x+1; printf(“x = %d\n”,x); Output x=1 x=2 x=3

External static variables An external static variable is declared outside of all functions and is available to all the functions in the program. An external static variable seems similar simple external variable but their difference is that static external variable is available only within the file where it is defined while simple external variable can be accessed by other files.

Static function Static declaration can also be used to control the scope of a function. If you want a particular function to be accessible only to the functions in the file in which it is defined and not to any function in other files, declare the function to be static. eg. static int power(int x inty) { . . . }

Register Variable These variables are stored in one of the machine’s register and are declared using register keyword. eg. register int count; Since register access are much faster than a memory access keeping frequently accessed variables in the register lead to faster execution of program. Since only few variable can be placed in the register, it is important to carefully select the variables for this purpose. However, C will automatically convert register variables into nonregister variables once the limit is reached. Don’t try to declare a global variable as register. Because the register will be occupied during the lifetime of the program.