Scope Rules and Storage Types CS-2303, C-Term 20101 Scope Rules and Storage Types CS-2303, System Programming Concepts (Slides include materials from The.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
C++ Programming: Program Design Including Data Structures, 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.
1 Review of Class on Oct Outline  Pointer  Pointers to void  Call-by-Reference  Basic Scope Rules  Storage Classes  Default Initialization.
Arrays in CCS-2301, B-Term Arrays in C (including a brief introduction to pointers) CS-2301, System Programming for Non-Majors (Slides include materials.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
Arrays in C & C++CS-2303, C-Term Arrays in C & C++ (including a brief introduction to pointers) CS-2303 System Programming Concepts (Slides include.
Introduction to FunctionsCS-2301 D-term Introduction to Functions CS-2301 System Programming C-term 2009 (Slides include materials from The C Programming.
Loose endsCS-2301, B-Term “Loose Ends” CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd.
Arrays in CCS-2301 B-term Arrays in C (with a brief Introduction to Pointers) CS-2301, System Programming for Non-majors (Slides include materials.
"Loose ends"CS-2301 D-term “Loose Ends” CS-2301 System Programming C-term 2009 (Slides include materials from The C Programming Language, 2 nd edition,
More on FunctionsCS-2301 B-term More on Functions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming Language,
Introduction to FunctionsCS-2301 B-term Introduction to Functions CS-2301, System Programming for Non-majors (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.
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
Symbolic Constants in CCS-2303, C-Term Digression – Symbolic Constants in C CS-2303, System Programming Concepts (Slides include materials from The.
Introduction to C++CS-2303, C-Term Introduction to C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Recursion and Function Implementation CS-2301 D-term Recursion and Implementation of Functions CS-2301 System Programming C-term 2009 (Slides include.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
CS-2303 System Programming Concepts
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Recursion and Implementation of Functions
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
Storage Classes.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
Basic Semantics Associating meaning with language entities.
Arrays in CCIS 1057 Fall Arrays in C (with a brief Introduction to Pointers) CIS 1057 Computer Programming in C Fall 2013 (Slides include materials.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
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.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ 16 September 2008.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
RecursionCIS 1057 Fall Recursion CIS 1057 Computer Programming in C Fall 2013 (Many slides based on/borrowed from Professor Hugh C. Lauer. Slides.
CHAPTER 8 Scope, Lifetime, and More on Functions.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
Advanced Programming in C
The Three Attributes of an Identifier
Functions Scope local global Global Resolution Operator part 5.
C Functions -Continue…-.
The Three Attributes of an Identifier
Storage class in C Topics Automatic variables External variables
CSC113: Computer Programming (Theory = 03, Lab = 01)
More On Enumeration Types
Storage class.
Scope Rules and Storage Types
Functions in C and C++ CS-2303 System Programming Concepts Hugh C. Lauer (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Scope Rules Of Variables
In C Programming Language
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
STORAGE CLASS.
The Three Attributes of an Identifier
C Programming Lecture-17 Storage Classes
Introduction to Classes and Objects
Presentation transcript:

Scope Rules and Storage Types CS-2303, C-Term Scope Rules and Storage Types CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)

Scope Rules and Storage Types CS-2303, C-Term Data Storage in Memory Variables may be automatic or static Automatic variables may only be declared within functions and compound statements (blocks) Storage allocated when function or block is entered Storage is released when function returns or block exits Parameters and result are (somewhat) like automatic variables Storage is allocated and initialized by caller of function Storage is released after function returns to caller.

Scope Rules and Storage Types CS-2303, C-Term Scope Identifiers declared within a function or compound statement are visible only from the point of declaration to the end of that function or compound statement. Like Java

Scope Rules and Storage Types CS-2303, C-Term Example int fcn (float a, int b) { int i; double g; for (i = 0; i < b; i++) { double h = i*g; loop body – may access a, b, i, g, h }// for(i…) fcn body – may access a, b, i, g }//int fcn( … ) i is visible from this point to end of fcn g is visible from this point to end of fcn h is only visible from this point to end of loop!

Scope Rules and Storage Types CS-2303, C-Term Idiosyncrasies In traditional C & Visual Studio All variables must be declared at beginning of function or compound statement (i.e., before first statement); visible from that point on In gcc Variables may be declared anywhere in function or compound statement; visible from that point on In C99 & C++ Loop variables may be declared in for statement; visible only to end of loop body, but not beyond

Scope Rules and Storage Types CS-2303, C-Term Static variables may be declared within or outside of functions Storage allocated when program is initialized Storage is released when program exits Static variables outside of functions may be visible to linker Compiler sets aside storage for all static variables at compiler or link time Values retained across function calls Initialization must evaluate to compile-time constant Static Data – Very different

Scope Rules and Storage Types CS-2303, C-Term Static Variable Examples int j;//static, visible to linker & all functs static float f; // not visible to linker, visible to // to all functions in this program int fcn (float a, int b) { // nothing inside of {} is visible to linker int i = b;//automatic double g;//automatic static double h;//static, not visible to // linker; value retained from call to call body – may access j, f, a, b, i, g, h }//int fcn( … )

Scope Rules and Storage Types CS-2303, C-Term Static Variable Examples (continued) int j;//static, visible to linker & all functs static float f; // not visible to linker, visible to // to all functions in this program int fcn (float a, int b) { // nothing inside of {} is visible to linker int i = b;//automatic double g;//automatic static double h;//static, not visible to // linker; value retained from call to call body – may access j, f, a, b, i, g, h }//int fcn( … ) Declaration outside any function:– always static static storage class:– not visible to linker

Scope Rules and Storage Types CS-2303, C-Term Static Variable Examples (continued) int j;//static, visible to linker & all functs static float f; // not visible to linker, visible to // to all functions in this program int fcn (float a, int b) { // nothing inside of {} is visible to linker int i = b;//automatic double g;//automatic static double h;//static, not visible to // linker; value retained from call to call body – may access j, f, a, b, i, g, h }//int fcn( … ) Inside function:– default is automatic static storage class:– not visible to linker

Scope Rules and Storage Types CS-2303, C-Term Static Variable Examples (continued) int j;//static, visible to linker & all functs static float f; // not visible to linker, visible to // to all functions in this program int fcn (float a, int b) { // nothing inside of {} is visible to linker int i = b;//automatic double g;//automatic static double h;//static, not visible to // linker; value retained from call to call body – may access j, f, a, b, i, g, h }//int fcn( … ) Note: value of h is retained from one call to next Value of h is also retained across recursions

Scope Rules and Storage Types CS-2303, C-Term Extern Variables int j;//static, visible to linker & all functs static float f; // not visible to linker, visible to // to all functions in this program extern float p; // static, defined in another program int fcn (float a, int b) { // nothing inside of {} is visible to linker int i = b;//automatic double g;//automatic static double h;//static, not visible to // linker; value retained from call to call body – may access j, f, a, b, i, g, h, p }//int fcn( … ) extern storage class:– a static variable defined in another C program

Scope Rules and Storage Types CS-2303, C-Term Extern Variables (continued) Examples:– stdin, stdout, stderr are extern variables that point to standard input, output, and error streams. extern variables:– Frequently occur in.h files. Each must be actually declared outside any function in exactly one.c file

Scope Rules and Storage Types CS-2303, C-Term Questions?