+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.

Slides:



Advertisements
Similar presentations
Storage class in C Topics Automatic variables External variables
Advertisements

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
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.
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.
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.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Modules and Scope Gabriel Hugh Elkaim Spring 2013.
C++ for Engineers and Scientists Third Edition
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.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using 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.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
18. DECLARATIONS.
Learners Support Publications Classes and Objects.
 2007 Pearson Education, Inc. All rights reserved C Functions -Continue…-
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
 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.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
#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.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
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.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Revisiting building. Preprocessing + Compiling 2 Creates an object file for each code file (.c ->.o) Each.o file contains code of the functions and structs.
Dynamic memory allocation and Intraprogram Communication.
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
Functions Course conducted by: Md.Raihan ul Masood
The Three Attributes of an Identifier
Chapter 7: User-Defined Functions II
Functions and an Introduction to Recursion
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.
The Three Attributes of an Identifier
Storage class in C Topics Automatic variables External variables
Chapter 5 - Functions Outline 5.1 Introduction
User-Defined Functions
C++ for Engineers and Scientists Second Edition
Chapter 5 - Functions Outline 5.1 Introduction
Scope, Parameter Passing, Storage Specifiers
Classes and Objects.
Storage class.
Chapter 7: User-Defined Functions II
Scope Rules Of Variables
Submitted By : Veenu Saini Lecturer (IT)
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
STORAGE CLASS.
The Three Attributes of an Identifier
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.
Presentation transcript:

+ Storage Classes and Linkage

+ Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared over several files of source code, Variables that can be used by any function in one particular file, Variables that can be used only within a particular function. Linkage External Internal No linkage Storage duration type variables that exist for the duration of a program Variables that exist only while the function containing them is executing. Store data in memory that is allocated and freed explicitly by means of function calls.

+ Scope Scope describes the region or regions of a program that can access an identifier. A C variable has one of the following scopes: block scope, function prototype scope, or file scope

+ Linkage Programs are built up out of multiple files and of course libraries. Functions in one file should be able to refer to functions (or other objects) in other files and libraries; A C variable has one of the following linkages: external linkage, internal linkage, no linkage (block scope or prototype scope). They are private to the block or prototype in which they are defined. A variable with file scope can have either internal or external linkage. A variable with external linkage can be used anywhere in a multifile program

+ Storage Duration A C variable has one of the following two storage durations: static storage duration or automatic storage duration. If a variable has static storage duration, it exists throughout program execution. Variables with file scope have static storage duration. The idea is that memory used for automatic variables is a workspace or scratch pad that can be reused. For example, after a function call terminates, the memory is used for its variables can be used to hold variables for the next function that is called.

+ The Five Storage Classes C uses scope, linkage, and storage duration to define five storage classes: automatic, register, static with block scope, static with external linkage, and static with internal linkage.

+ Automatic Variables A variable belonging to the automatic storage class has automatic storage duration, block scope, and no linkage. By default, any variable declared in a block or function header belongs to the automatic storage class. Recall that automatic storage duration means that the variable comes into existence when the program enters the block that contains the variable declaration. When the program exits the block, the automatic variable disappears. Its memory location can now be used for something else. A variable is known only to the block in which it is declared and to any block inside that block:

+ Blocks Without Braces An entire loop is a sub-block to the block containing it, and the loop body is a sub-block to the entire loop block. Similarly, an if statement is a block, and its associated substatement is a sub-block to the if statement. These rules affect where you can declare a variable and the scope of that variable.

+ Register Variables Variables are normally stored in computer memory. Register variables are stored in the CPU registers or, more generally, in the fastest memory available. Declaring a variable as a register class is more a request than a direct order. You can't take the address of a register variable

+ Static Variables with Block Scope Note that trystat() increments each variable after printing its value. Running the program returns this output fade is initialized each time trystat() is called, but stay is initialized just once, when trystat() is compiled. Static variables are initialized to zero if you don't explicitly initialize them to some other value.

+ Static Variables with External Linkage A static variable with external linkage has file scope, external linkage, and static storage duration. This class is sometimes termed the external storage class, and variables of this type are called external variables 1.You create an external variable by placing a defining declaration outside of any function. 2.An external variable can additionally be declared inside a function that uses it by using the extern keyword

+ Examples Example 1 contains one external variable: Hocus. It is known to both main() and magic().  Example 2 has one external variable, Hocus, known to both functions. This time, magic() knows it by default.

+ Example The Hocus variable in main() is automatic by default and is local to main. The Hocus variable in magic() is automatic explicitly and is known only to magic(). The external Hocus variable is not known to main() or magic() but would be known to any other function in the file that did not have its own local Hocus. Finally, Pocus is an external variable known to magic() but not to main() because Pocus follows main().

+ Storage-Class Specifiers The auto specifier indicates a variable with automatic storage duration. It can be used only in declarations of variables with block scope, which already have automatic storage duration, so its main use is documenting intent. The register specifier also can be used only with variables of block scope. It puts a variable into the register storage class, which amounts to a request that the variable be stored in a register for faster access. It also prevents you from taking the address of the variable. The static specifier, when used in a declaration for a variable with block scope, gives that variable static storage duration so it exists and retains its value as long as the program is running, even at times the containing block isn't in use. The variable retains block scope and no linkage. When used in a declaration with file scope, it indicates that the variable has internal linkage. The extern specifier indicates that you are declaring a variable that has been defined elsewhere. If the declaration containing extern has file scope, the variable referred to must have external linkage. If the declaration containing extern has block scope, the referred-to variable can have either external linkage or internal linkage, depending on the defining declaration for that variable.

+ Storage Classes and Functions Functions, too, have storage classes. An external function can be accessed by functions in other files, but a static function can be used only within the defining file. The functions gamma() and delta() can be used by functions in other files that are part of the program, but beta() cannot. Because this beta() is restricted to one file, you can use a different function having the same name in the other files.