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.

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.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 9 MEMORY MANAGEMENT.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Scope Rules and Storage Types CS-2303, C-Term Scope Rules and Storage Types CS-2303, System Programming Concepts (Slides include materials from The.
 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.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
Storage Classes.
ECE 103 Engineering Programming Chapter 10 Variables, AKA Objects Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103.
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.
chap13 Chapter 13 Programming in the Large.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
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.
Compiler Construction
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
ECE 103 Engineering Programming Chapter 9 gcc Compiler Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
 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.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
ECE 103 Engineering Programming Chapter 49 Structures Unions, Part 1 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
Lecture 05 Functions II, Storage Class, Scope, rand() METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet.
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
CSCI 171 Presentation 6 Functions and Variable Scope.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
ECE 103 Engineering Programming Chapter 41 C Pointers, Part 3 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
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.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Advanced Programming in C
The Three Attributes of an Identifier
Chapter 7: User-Defined Functions II
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.
Storage class in C Topics Automatic variables External variables
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
Functions: Declaration, Definition, Call and return, Scope of variables, Storage classes, Recursive functions, Recursion vs Iteration.
Storage class.
Scope Rules and Storage Types
Chapter 7: User-Defined Functions II
ECE 103 Engineering Programming Chapter 19 Nested Loops
ECE 103 Engineering Programming Chapter 12 More C Statements
Scope Rules Of Variables
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
In C Programming Language
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.
Presentation transcript:

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 developed by Professor Phillip PSU ECE

Syllabus Variable Storage Classes Memory Storage Layout Storage Classes in Multiple Files Storage Classes for Functions

2 Variable Storage Classes Storage class of a variable determines its:  Scope  Lifetime C storage classes are:  auto  static  register  extern

3 Scope and Storage:  C variable v has scope and storage attributes  The storage attribute states, how long v lives  The scope attributes states, where v is visible Storage attributes can be static, auto, or register Scope attributes can be local, global, or external:  Local means, v is only visible inside the current, innermost scope, independent of storage attribute; e.g. there are local static variables in C  Global means, v is visible in the whole compilation unit, from the line of declaration to the end of file  External means, v is visible in this and in other compilation units, and its storage attribute is static

4 auto Storage Class auto is the default storage class for a variable defined inside a function body or a statement block The auto prefix is optional; i.e. any locally declared variable is automatically auto unless specifically defined to be static Example: { auto double x; /* Same as: double x */ int num; /* Same as: auto int num; */... }

5 Storage for an auto variable is:  Allocated when control enters the variable’s containing block  Released when control leaves its containing block Example: { int x; { int y = 1; } } enter blockstorage created for x enter blockleave block – storage for y releasedstorage created for y leave block – storage for x released

6 If an auto variable is defined but not initialized:  Variable has an unknown value when control enters its containing block If an auto variable is defined and initialized at the same time:  Variable is re-initialized each time control enters its containing block An auto variable’s scope is limited to its containing block (i.e., it is local to the block)

7 static Storage Class Storage for a static variable:  Is allocated when execution begins  Exists for as long as the program is running A static variable may be defined either inside or outside a function’s body. The static prefix must be included Example: static double seed;

8 If a static variable is defined but not initialized:  Is set to zero (0) once, when storage is allocated If a static variable is simultaneously defined and initialized:  Is initialized once, when storage is allocated A static variable defined inside a function body is visible only in its containing block A static variable defined outside a function body is visible to all blocks which follow it in the current compilation units If you wish it to be visible in other compilation units, it must be declared extern

9 Example: #include void weird( int x ) { // weird static int y; /* Persistent */ if ( x == 0 ) printf( "%d\n", y ); else if ( x == 1 ) y = 100; else if ( x == 2 ) y++; } //end weird int main (void) { // main weird(1); /* Set y in weird to 100 */ weird(0); /* Will display 100 */ weird(2); /* Increment y in weird */ weird(0); /* Will display 101 */ return 0; } // end main

10 register Storage Class The fastest storage resides within the CPU itself in high-speed memory cells called registers The programmer can request the compiler to use a CPU register for storage Example: register int k; The compiler can ignore the request, in which case the storage class defaults to auto Some machines –e.g. stack architectures– have no user visible register

11 extern Storage Class (single source file) extern is the default storage class for a variable defined outside a function’s body Storage for an extern variable:  Is allocated when execution begins  Exists for as long as the program is running

12 If an extern variable is defined but not initialized:  Is set to zero (0) once, when storage is allocated If an extern variable is simultaneously defined and initialized:  Is initialized once, when storage is allocated An extern variable is visible in all functions that follow its definition (i.e., it is global)

13 Example: #include float x = 1.5; /* Definition - extern class - global */ void disp (void) { printf("%f\n", x); /* Access global x */ } int main (void) { printf("%f\n", x); /* Access global x */ disp(); return 0; }

14 Memory Storage Layout Where are auto, static, and extern variables stored? Code Segment (Text Segment) Contains the program’s machine code Data SegmentContains static data (e.g., static class, extern globals) Stack SegmentContains temporary data (e.g., auto class) free Unallocated memory that the stack and heap can use Heap SegmentContains dynamically allocated data (e.g., via malloc)

15 Storage Classes in Multiple Files Functions stored in a single source file can be divided into separate source files. Variables defined in one source file can be accessed from other source files via the extern storage class. An extern variable can be defined in one file only. However, it may be declared from other files.

16 An extern variable is defined exactly once in a file by placing it outside all blocks.  If an extern variable is not initialized at definition time → extern prefix must be omitted  If an extern variable is initialized at definition time → extern prefix is optional An extern variable is declared in another file by using the extern prefix. Example: extern int k;

17 Filename: ex1_A.c #include double pi = ; /* Definition of global variable here */ double deg_to_rad (double deg); /* Prototype */ int main (void) { printf("%f\n", deg_to_rad(45.0)); return 0; } Filename: ex1_B.c extern double pi; /* Declaration allows it to be used here */ double deg_to_rad (double deg) { return deg * pi / 180; }

18 Storage Classes for Functions extern is the default storage class for a function. The extern prefix is usually omitted from the function’s definition. An extern function can be invoked by any other function in any file. If a function is invoked in one file but is defined in a different file, then declare its prototype in the file where the function is invoked.

19 Filename: ex2_A.c #include int a = 123;/* Global in ex2_A.c */ void fun1 (int x);/* Prototype for fun1 in ex2_A.c */ void fun2 (void);/* Prototype for fun2 in ex2_B.c */ int main (void) { int a = 2;/* Local to main */ printf("1:main: a= %d\n", a); fun1(a); fun2(); printf("8:main: a= %d\n", a); a = 99; printf("9:main: a= %d\n", a); return 0; }

20 continued … this code is still part of ex2_A.c void fun1 (int x) { printf("2:fun1: x= %d\n", x); x = 21; printf("3:fun1: a= %d\n", a); { int a;/* Local to this block in fun1 */ a = 5; printf("4:fun1: a= %d\n", a); } printf("5:fun1: a= %d\n", a); }

21 Filename: ex2_B.c #include extern int a;/* Declare variable that resides in ex2_A.c */ void fun2 (void) { printf("6:fun2: a= %d\n", a); a = -99; printf("7:fun2: a= %d\n", a); }

22 Compile the source files using gcc: gcc ex2_A.c ex2_B.c or gcc *.c Actual output: 1:main: a= 2 2:fun1: x= 2 3:fun1: a= 123 4:fun1: a= 5 5:fun1: a= 123 6:fun2: a= 123 7:fun2: a= -99 8:main: a= 2 9:main: a= 99