In C Programming Language

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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
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.
Lifetime “The lifetime of a variable is the time during which the variable is bound to a specific memory location.” [p. 219] “…the lifetime of a variable.
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.
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.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
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.
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.
Senem Kumova Metin // CS115 // FUNCTIONS continues CHAPTER 5.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
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.
 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.
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.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Dynamic memory allocation and Intraprogram Communication.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Chapter 7: User-Defined Functions II
C Functions -Continue…-.
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
A Lecture for the c++ Course
Storage class in C Topics Automatic variables External variables
Pointers.
trigraph ??= is for # ??( is for [ ??< is for { ??! is for |
Module 5 Sorting and Searching: Bubble sort Selection sort
Introduction to C Programming Language
Programmazione I a.a. 2017/2018.
Dynamic memory allocation and Intraprogram Communication
Dynamic memory allocation and Intraprogram Communication
Scope, Parameter Passing, Storage Specifiers
Advanced Programming Basics
CS 240 – Lecture 5 Scope of Variables, The Stack, Automatic Variables, Global Variables, Constant Type.
פרטים נוספים בסילבוס של הקורס
Functions: Declaration, Definition, Call and return, Scope of variables, Storage classes, Recursive functions, Recursion vs Iteration.
פרטים נוספים בסילבוס של הקורס
Pointers.
Govt. Polytechnic,Dhangar
C Storage classes.
Storage class.
Dr Tripty Singh Tutorial for Fuctions
Chapter 7: User-Defined Functions II
Scope Rules Of Variables
STORAGE CLASS.
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:

In C Programming Language Storage Class In C Programming Language

Introduction Basically, computer allocates space for variables in TWO ways Memory CPU Registers

Why Storage Classes? Where the variable would be stored? Memory or CPU Registers What will be the initial value of the variable? i.e., default value (Starting value) What is the scope of the variable? For which function the value of the variable would be available What is the life time of a variable? How long would be variable exists

Types Storage Classes There are FOUR types of storage classes Automatic Storage class (auto) Register Storage class (register) Static Storage class (static) External Storage class (extern)

Automatic Storage Class Memory Default value Garbage value Scope Local to the block in which the variable is defined Life time Till the control remains within the block in which variable is defined

Example 1 #include<stdio.h> int main(){ int i; auto char c;     float f;     printf("%d  %c  %f",i,c,f);     return 0; } Output: Garbage Garbage Garbage

Example 2 Output: 20 10 #include<stdio.h> int main(){ int a=10;     {          int a=20;          printf("%d",a);     }     printf(" %d",a);     return 0; } Output: 20 10

Example 3 #include<stdio.h> int main(){ { int a=20; printf("%d",a); } printf(" %d",a); //a is not visible here return 0; } Output: Compilation error

Example 4 #include<stdio.h> int main(){ int i; for(i=0;i<4;i++){ int a=20; printf("%d",a); a++; } return 0; } Output: 20 20 20 20

Register Storage Class Default value Garbage value Scope Local to the block in which the variable is defined Life time Till the control remains within the block in which variable is defined

Example 1 #include<stdio.h> int main(){ register int a=10; int *p; p=&a; printf("%u",p); } Output: Compilation error

Example 2 #include<stdio.h> int main(){ register int a,b; scanf("%d%d",&a,&b); printf("%d %d",a,b); } Output: Compilation error

Static Storage Class Storage Default value Scope Life time Memory Zero Local to the block in which the variable is defined Life time The value of the persists between different function calls (i.e., Initialization is done only once)

Example 1 #include<stdio.h> int a; int main(){ printf("%d",a); return 0; } Output: 0

Example 2 #include<stdio.h> static int a; int main(){ printf("%d",a); return 0; } Output: 0

Example 3 #include <stdio.h> static char c; static int i; static float f; int main(){ printf("%d %d %f",c,i,f); return 0; } Output: 0 0 0.000000

Example 4 #include <stdio.h> static int i=10; int main(){ i=25; //Assignment statement printf("%d",i); return 0; } Output: 25

Example 5 #include<stdio.h> int main(){ { static int a=5; printf("%d",a); } //printf("%d",a); variable a is not visible here. return 0; } Output: 5

External Storage Class Memory Default value Zero Scope Global (i.e., Throughout the program ) Life time As long as the program’s execution does not comes to end

Example 1 #include <stdio.h> int i; //By default it is extern variable int main(){ printf("%d",i); return 0; } Output: 0

Example 2 #include <stdio.h> extern int i; //extern variable int main(){ printf("%d",i); return 0; } Output: Compilation error, undefined symbol i.

Example 3 #include <stdio.h> void sum(int,int) //By default it is extern. int main(){ int a=5,b=10; sum(a,b); return 0; } void sum(int a,int b){ printf("%d”",a+b); Output: 15

Example 4 #include <stdio.h> extern int i=10; //extern variable int main(){ printf("%d",i); return 0; } Output: 10

Example 5 #include <stdio.h> int main(){ extern int i=10; //Try to initialize extern variable locally. printf("%d",i); return 0; } Output: Compilation error: Cannot initialize extern variable.