University of Kelaniya Sri Lanka

Similar documents
Fundamentals of Programming Session 12

Storage class in C. Automatic variables External variables Static variables Register variables Scopes and longevity of above types of variables.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

M1-R4: Programing and Problem Solving using C (JAN 2019)

Programming Languages

Day08 A. Young W. Lim Mon. Young W. Lim Day08 A Mon 1 / 27

Unit 7. Functions. Need of User Defined Functions

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Lecture 3: C Programm

A Fast Review of C Essentials Part II

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

M.EC201 Programming language

Function Call Stack and Activation Records

Week 4 Lecture 1. Expressions and Functions

Memory Allocation in C

CMPE-013/L. Introduction to C Programming. Unit testing

AMCAT Automata Coding Sample Questions And Answers

Contents. 8-1 Copyright (c) N. Afshartous

Variation of Pointers

User Defined Functions

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

Lecture 3. Variables. Variables

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

15 FUNCTIONS IN C 15.1 INTRODUCTION

C: How to Program. Week /Apr/23

Chapter 9. Variable Scope and Functions

Computers Programming Course 5. Iulian Năstac

Object-Oriented Principles and Practice / C++

Motivation was to facilitate development of systems software, especially OS development.

CS201 Some Important Definitions

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Lecture 2: C Programm

In Java we have the keyword null, which is the value of an uninitialized reference type

Memory Usage in Programs

In addition to name. Each variable in C program is characterized by its Type Scope Storage Class. Type Have already discussed

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Technical Questions. Q 1) What are the key features in C programming language?

Creating, Compiling and Executing

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

Motivation was to facilitate development of systems software, especially OS development.

Introduction to C++ with content from


The Three Attributes of an Identifier

Example : class Student { int rollno; float marks; public: student( ) //Constructor { rollno=0; marks=0.0; } //other public members };

C Programming Language

C++ (classes) Hwansoo Han

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

SISTEMI EMBEDDED. Stack, Subroutine, Parameter Passing C Storage Classes and Scope. Federico Baronti Last version:

Functions and Recursion

Computers Programming Course 6. Iulian Năstac

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

Homework #3 CS2255 Fall 2012

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory

Functions and Recursion

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

BLM2031 Structured Programming. Zeyneb KURT

OER on Loops in C Programming

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

ENERGY 211 / CME 211. Functions

UNIT 3 FUNCTIONS AND ARRAYS

Pointers, Dynamic Data, and Reference Types

C: How to Program. Week /Apr/16

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

CS113: Lecture 4. Topics: Functions. Function Activation Records

(2) Accidentally using the wrong instance of a variable (sometimes very hard one to find).

Chapter 5. Names, Bindings, and Scopes

CS240: Programming in C

Data Structure Series

Class Information ANNOUCEMENTS

PROGRAMMAZIONE I A.A. 2017/2018

Advances in Programming Languages: Regions

CPSC 427: Object-Oriented Programming

C++ Programming for Non-C Programmers. Supplement

QUIZ. What is wrong with this code that uses default arguments?

Functions. (transfer of parameters, returned values, recursion, function pointers).

Object-Oriented Programming for Scientific Computing

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Procedural programming with C

A Fast Review of C Essentials Part I

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

C++ Programming for Non-C Programmers. Supplement

QUIZ. Source:

CS201- Introduction to Programming Current Quizzes

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Review of the C Programming Language for Principles of Operating Systems

DECLARAING AND INITIALIZING POINTERS

UNIT III (PART-II) & UNIT IV(PART-I)

Computers in Engineering. Moving From Fortran to C Michael A. Hawker

OBJECTIVE QUESTIONS: Choose the correct alternative:

Transcription:

University of Kelaniya Sri Lanka Scope, Lifetime and Storage Class of a Variable COSC 12533/ COST 12533 SACHINTHA PITIGALA 2017 - Sachintha Pitigala < 1

What is Scope? Scope of Identifier: The scope of an identifier (or named constant) is the region of program code in which it is legal to use that identifier for any purpose. < 2

Scope of a Variable There are two places where variables can be declared in C programming language: 1. Inside a function or a block which is called local variables 2. Outside of all functions which is called global variables. < 3

Local Variables Variables that are declared inside a function are called local variables. They can be used only by statements that are inside that function. Local variables are not known to functions outside their own. < 4

Local Variables: Examples < 5

More Examples: In C, you can reuse names, as long as they are not in overlapping scopes. void main ( ) { int i = 5, j = 0; for (j = 0; j < 10; j++) { int i = j; // OK, this is new i int k = 5; dosomething (i); int sum = k; // compile error, no k in scope j = i; // sets j to 5 for (j = 0; j < 100; j++ ) { int i = j; // yet another new i int i = 0; // compile error redefined variable < 6

Global Variables The global variables need to be declared outside of all functions. Global variables are available from the point of declaration to the end of the entire file containing the program code. Try to minimize global scope: Only use global variables if you really, really have to!!! < 7

Example: int i = 10; void main ( ) { i =5; for (j = 0; j < 10; j++ ) { int i = 20; i=70; i=90; int i = 30; i=100; < 8

Lifetime of a Variable The lifetime of a variable is the time during program execution in which a variable actually has memory allocated to it. < 9

Lifetime of Local Variables Their storage is created (allocated) when control enters the function. Local variables are alive while function is executing. Their storage is destroyed (deallocated) when function exits. < 10

Lifetime of Global Variables Their lifetime is the lifetime of the entire program. Their memory is allocated when program begins execution. Their memory is deallocated when the entire program terminates. < 11

Storage Classes in C There are four types of storage classes in C: 1. Automatic 2. Static 3. Extern 4. Register However, we will talk only the first three storage classes in this course. < 12

Automatic Storage Class Automatic variables are declare inside a function in which they are to be utilized. Automatic variables are declared using a keyword auto. eg. auto int number; These variables are created when the function is called and destroyed automatically when the function is exited. These variables are therefore private(local) to the function in which they are declared. < 13

Automatic Storage Class auto is the default storage class for all local variables. { int Count; auto int Month; The example above defines two variables with the same storage class. auto can only be used within functions, i.e. local variables. < 14

Static Storage Class 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; Static variables are initialized only once. < 15

Static Class Example: #include <stdio.h #include <stdlib.h void increment(); void main() { increment(); increment(); increment(); void increment() { static int i=1; #include <stdio.h #include <stdlib.h void increment(); void main() { increment(); increment(); increment(); void increment() { static int i=1; printf("%d\t",i); printf("%d\t",i); i++; Output: 1 1 1 i++; Output: 1 2 3 < 16

External Storage Class 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. < 17

External declaration: example void main() { extern int y;...... void func1() { extern int y;...... int y; Note that extern declaration does not allocate storage space for variables. < 18