1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.

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) Scope a] Ada scope rules b] C scope rules 2) Parameter passing a] Ada parameter modes b) Parameter passing mechanisms COMP205 IMPERATIVE LANGUAGES 13.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
1 Review of Class on Oct Outline  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
The different types of variables in a Java program.
Storage Classes.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Modules and Scope Gabriel Hugh Elkaim Spring 2013.
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.
MAHENDRAN CHAPTER 6. Session Objectives Explain Type of Functions Discuss category of Functions Declaration & Prototypes Explain User Defined Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
18. DECLARATIONS.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Week 6: Functions - Part 2 BJ Furman 01OCT2012. The Plan for Today Comments on midterm exam (next week in lab!) Review of functions Scope of identifiers.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
 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.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
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.
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.
Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
Lecture 7: Variable Scope B Burlingame March 16, 2016.
Dynamic memory allocation and Intraprogram Communication.
The Three Attributes of an Identifier
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.
ㅎㅎ Fourth step for Learning C++ Programming Namespace Function
A Lecture for the c++ Course
Storage class in C Topics Automatic variables External variables
trigraph ??= is for # ??( is for [ ??< is for { ??! is for |
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
CS 240 – Lecture 5 Scope of Variables, The Stack, Automatic Variables, Global Variables, Constant Type.
C Storage classes.
Storage class.
In C Programming Language
1-6 Midterm Review.
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:

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material

2 Each variable in a C program has 2 properties Storage Duration –Is the portion of the program execution during which storage for the variable exists –Can be temporary (automatic) or permanent Scope –Is the portion of the code in which the variable can be referenced –Can be block scope or file scope Storage Duration & Scope

3 Local and Global Variables Broadly speaking all variables in C can be classified as follows: Local –Only meaningful inside the function or the code block (compound statement) that declares them –Block scope Global –Their declaration is outside of any function –Can be used by all functions in a program –Permanent storage and File scope –This goes against modularity –Avoid using them!

4 Local and Global Variables #include int global; /* global var: permanent storage, file scope */ void print_it(void){ int local = 3; /* local var: auto storage, block scope */ printf("global = %d, local: %d\n", global, local); } /* end-print_it */ int main(void){ int local = 5; /* temporary (auto) storage, block scope */ global = local * local; print_it(); return 0; } /* end-main */ global = 25, local: 3

5 Local&Global Vars: Another Example #include int sum; /* global var: permanent storage, file scope */ void Sum(int n){ /* parameter n: auto storage, block scope */ int i; /* local var: auto storage, block scope */ sum = 0; /* Access a global var */ for (i=1; i<=n; i++) sum +=i; } /* end-Sum */ void main(void){ int sum1; /* local var: auto storage, block scope */ int sum2; /* local var: auto storage, block scope */ Sum(10); sum1 = sum; /* Access a local var + global var */ Sum(20); sum2 = sum; /* Access a local var + global var */ printf(sum1: %d, sum2: %d\n, sum1, sum2); } /* end-main */

6 Automatic Variables #include int factorial(auto int n); int main() { auto int n = 5; printf("n! = %d\n", factorial(n)); printf("2! = %d\n", factorial(2)); } int factorial(auto int n) { auto int prod = 1; for( ; n > 1; n--) prod *= i ; return prod; } n! = 120 2! = 2 Declared within a function and are local to the function in which they are declared, that is, the scope of the variable is confined to that function Do not retain their value once the control is transferred out of its defining function (any value assigned to an automatic variable within a function will be lost once the function is exited) auto keyword may be omitted

7 Static Variables #include /* This is a buggy factorial function */ int factorial(int n) { static int prod = 1; for( ; n > 1; n--) prod *= n; return prod; } int main() { int n = 5; printf("n! = %d\n", factorial(n)); printf("2! = %d\n", factorial(2)); } n! = 120 2! = 240 Static variables are like global variables except that they are only accessible in the code block where they are declared Static variables retain their value between calls

8 External Global Variables If the program code is spread over multiple files, you may want to refer to a global variable declared in another file –To do this, you declare the variable extern in the file where you want to refer to it a.c int x; /* Global var */ void main(void){ extern void f(void); f(); printf(x: %d\n, x); } b.c extern int x; /* external global var * means that int x is declared * in some other file, and I want * to refer to it here */ void f(void){ x = 2; }

9 Register Variables register keyword is used to make code more efficient Results are compiler-dependent register variables use CPU registers (rather than RAM memory) whenever compiler can Good compilers do this anyway register int sum = 0; register int i; for (i=1; i <= 100; i++) sum += i;