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

Similar documents
Unit 7. Functions. Need of User Defined Functions

University of Kelaniya Sri Lanka

UNIT 3 FUNCTIONS AND ARRAYS

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

Fundamentals of Programming Session 12

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

Functions and Recursion

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

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

M.EC201 Programming language

A Fast Review of C Essentials Part II

15 FUNCTIONS IN C 15.1 INTRODUCTION

LESSON 6 FLOW OF CONTROL

Functions. Chapter 5

CS240: Programming in C

Lecture 3. Variables. Variables

Flow Control. CSC215 Lecture

Lecture 3: C Programm

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

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

OBJECTIVE QUESTIONS: Choose the correct alternative:

CSE 230 Intermediate Programming in C and C++ Functions

Model Viva Questions for Programming in C lab

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

Memory Allocation in C

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

Computer Programming Unit 3

Problem Solving and 'C' Programming

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

Functions BCA-105. Few Facts About Functions:

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

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

Lecture 2: C Programm

Procedures, Parameters, Values and Variables. Steven R. Bagley

Structured Programming. Functions and Structured Programming. Functions. Variables

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

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

Lecture 02 C FUNDAMENTALS

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

Scope. Scope. Region of a program in which a defined object is visible. Defined Objects. Two types of regions. Variables Functions

Introduction. C provides two styles of flow control:

Principles of C and Memory Management

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

Variables and Operators 2/20/01 Lecture #

BLM2031 Structured Programming. Zeyneb KURT

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

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Questions Bank. 14) State any four advantages of using flow-chart

PROGRAMMAZIONE I A.A. 2017/2018

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Variation of Pointers

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

Chapter 8: Intraprogram Communication. Lecture 8 1

C-LANGUAGE CURRICULAM


Storage class and Scope:

Review of the C Programming Language

C Programming Language

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

COMP 208 Computers in Engineering

CSE 5A Final Fall 2006

A Fast Review of C Essentials Part I

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

Review of the C Programming Language for Principles of Operating Systems

Introduction to C Language (M3-R )

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

AMCAT Automata Coding Sample Questions And Answers

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

CSCE 206: Structured Programming in C++

FUNCTIONS. Without return With return Without return With return. Example: function with arguments and with return value

This exam is to be taken by yourself with closed books, closed notes, no calculators.

Goals of this Lecture

Chapter 2 - Introduction to C Programming

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Review for Test 1 (Chapter 1-5)

Decision Making and Loops

Programming Fundamentals (CS-302 )

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

Computers Programming Course 5. Iulian Năstac

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

CS240: Programming in C

CSC 533: Organization of Programming Languages. Spring 2005

Computers Programming Course 7. Iulian Năstac

DECISION MAKING STATEMENTS


Model Viva Questions for Programming in C lab

PROGRAMMAZIONE I A.A. 2017/2018

Darshan Institute of Engineering & Technology for Diploma studies Unit 4

Unit 3 Decision making, Looping and Arrays

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

First of all, it is a variable, just like other variables you studied

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

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

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

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Creating, Compiling and Executing

C: How to Program. Week /Mar/05

Class Information ANNOUCEMENTS

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Transcription:

1

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

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

Storage -- Memory Automatic storage class Default initial value garbage(unpredictable) value Scope Local to the block Life time Till control is within that block in which the variable is defined 4

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

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. 6

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. 7

External Variables Storage -- Memory Default initial value Zero Scope Global Life time As long as the program s execution doesn t come to an end 8

External variable (examples) int number; float length=7.5; main() funtion1() funtion1() The variable number and length are available for use in all three function 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. 9

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; return(x); 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 10

External declaration int main() y=5; int y; 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 1. Define y before main. 2. Declare y with the storage class extern in main before using it. func1() y=y+1 11

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

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

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

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. 15

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. 16

Storage -- Memory Static Variables Default initial value Zero Scope Local to the block Life time Persists between various function calls 17

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 18

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. 19

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) 20

Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static. Another reason for making functions static can be reuse of the same function name in other files. For example, if we store following program in one file file1.c /* Inside file1.c */ static void fun1(void) puts("fun1 called"); And store following program in another file file2.c /* Iinside file2.c */ int main(void) fun1(); getchar(); return 0; Now, if we compile the above code with command gcc file2.c file1.c, we get the error undefined reference to `fun1. This is because fun1() is declared static in file1.c and cannot be used infile2.c. 21

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 non-register 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. 22

Storage -- CPU registers Register storage class Default initial 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 the variable is defined 23

Q1. int num; main() int i, j; for (i = 1; i <= 3; i++) j = increment(); printf ("j = %d\n", j); printf ("num = %d", num); Increment() num++; return (num); j=1 j=3 num=3 Q2. main() int i; for (i = 1 ; 100;i++) printf ("%d\n", i); infinite loop Q3. main() char ch = 122, ch1 = z ; printf ("ch = %c,ch); printf Cch1=%d", ch1); O/p ch=z ch1=122 24

Q4. main() printf ("in main i = %d\n", i); func(); int i = 5; func() printf ("In fund i = %d\n", i); Error: undefined symbol i Q2. int i; main() int j; for(;;) if (j = function (i)) printf( j=%d\n,j); else break; function (x) int x; static int v = 2; v--; return(v-x); j=1 25