The Three Attributes of an Identifier

Similar documents
Fundamentals of Programming Session 12

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

A Fast Review of C Essentials Part II

C Programming Language

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

University of Kelaniya Sri Lanka

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

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

// file2.c. // file1.c #include <stdio.h> int A1 = 42; // 1.1 static int B1; // 1.2. int A2 = 12; // 2.1 int B2; // 2.2. extern int A2; // 1.

Iterative Languages. Scoping

Functions. Cedric Saule

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so! printed

Chapter 8: Intraprogram Communication. Lecture 8 1

CS240: Programming in C

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Coding Standards for C

Coding Standards for C

Variation of Pointers

15 FUNCTIONS IN C 15.1 INTRODUCTION

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

CSE 333 Lecture 7 - final C details

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

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

CS240: Programming in C

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012

CS 2505 Computer Organization I Test 1. Do not start the test until instructed to do so! printed

M.EC201 Programming language

A Fast Review of C Essentials Part II

Memory Allocation in C

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

Dynamic Memory CMSC 104 Spring 2014, Section 02, Lecture 24 Jason Tang

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

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

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

Programming Languages

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

PROGRAMMAZIONE I A.A. 2017/2018

CE221 Programming in C++ Part 1 Introduction

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

LESSON 6 FLOW OF CONTROL

Learning C Language. For BEGINNERS. Remember! Practice will make you perfect!!! :D. The 6 th week / May 24 th, Su-Jin Oh

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

Unit 7. Functions. Need of User Defined Functions

Contents. 8-1 Copyright (c) N. Afshartous

Declaration of Statically-Allocated Arrays

Object Oriented Design

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

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

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

make and makefile CS 211

Function Call Stack and Activation Records

An Introduction to LEX and YACC. SYSC Programming Languages

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

CSCI 2132 Software Development. Lecture 19: Generating Permutations

Today s class. Finish review of C Process description and control. Informationsteknologi. Tuesday, September 18, 2007

Binding and Variables

COMP322 - Introduction to C++

Computer Programming Practice (2008 Winter) Practice 10 C Scoping & Pointer

Pointer Arithmetic and Lexical Scoping. CS449 Spring 2016

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

CS240: Programming in C

Generating PWM on LPCXpresso - LPC1769

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

CS561 Manju Muralidharan Priya Structures in C CS200 STRUCTURES. Manju Muralidharan Priya

Lecture 2. Examples of Software. Programming and Data Structure. Programming Languages. Operating Systems. Sudeshna Sarkar

Functions. Arash Rafiey. September 26, 2017

Today's Topics. CISC 458 Winter J.R. Cordy

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

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

Intermediate Programming, Spring 2017*

CSSE 332 Standard Library, Storage classes, and Make

CS 2505 Computer Organization I

C: How to Program. Week /Apr/16

Intermediate Programming, Spring 2017*

Lecture 04 FUNCTIONS AND ARRAYS

CSE 333 Autumn 2014 Midterm Key

CSE 230 Intermediate Programming in C and C++ Functions

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

Kurt Schmidt. October 30, 2018

Full file at C How to Program, 6/e Multiple Choice Test Bank

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

Functions. Systems Programming Concepts

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

Basic Elements of C. Staff Incharge: S.Sasirekha

Review of the C Programming Language for Principles of Operating Systems

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

Scope: Global and Local. Concept of Scope of Variable

CSE 333 Midterm Exam 7/27/15 Sample Solution

CSE 333 Midterm Exam 5/9/14 Sample Solution

FUNCTIONS POINTERS. Pointers. Functions

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo

Example: Pointer Basics

Personal SE. Functions, Arrays, Strings & Command Line Arguments

A Crash Course in C. Steven Reeves

Introduction to C An overview of the programming language C, syntax, data types and input/output

More C++ : Vectors, Classes, Inheritance, Templates

BLM2031 Structured Programming. Zeyneb KURT

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

EL6483: Brief Overview of C Programming Language

Transcription:

The Three Attributes of an Identifier Identifiers have three essential attributes: storage duration scope linkage Identifier Attributes 1

Storage Duration storage duration Identifier Attributes 2 determines when memory is set aside for the variable and when that memory is released automatic allocated when the surrounding block is executed deallocated when the block terminates static stays in the same storage location as long as the program is running can retain its value indefinitely (until program terminates)

Automatic Storage Duration automatic allocated when the surrounding block is executed deallocated when the block terminates Identifier Attributes 3 void Sort(int list[], int Sz) { int startidx = 0; declared inside a block created (memory allocated) on each call initialized on each call deallocated (memory reclaimed) when call ends

Static Storage Duration static Identifier Attributes 4 stays in the same storage location as long as the program is running can retain its value indefinitely (until program terminates) int numcallstosort = 0; void Sort(int list[], int Sz) { declared outside all blocks initialized once, keeps its value until program ends static int numswaps = 0; declared inside a block, with keyword static initialized once, keeps its value from one call to the next

Scope scope Identifier Attributes 5 (of an identifier) the range of program statements within which the identifier is recognized as a valid name block scope visible from its point of declaration to the end of the enclosing block place declaration within a block file scope visible from its point of declaration to the end of the enclosing file place declaration outside of all blocks (typically at beginning of file)

Block Scope block scope Identifier Attributes 6 visible from its point of declaration to the end of the enclosing block place declaration within a block void Sort(int list[], int Sz) { static int numswaps = 0; int startidx = 0; for ( ) { int stopidx = ; declared inside a block can only be referred to from declaration to end of block return;

File Scope file scope Identifier Attributes 7 visible from its point of declaration to the end of the enclosing file place declaration outside of all blocks (typically at beginning of file) int numcallstosort = 0; void Sort(int list[], int Sz) { declared outside all blocks can be referred to from any function within the file potentially dangerous avoid unless necessary pass parameters instead

Linkage linkage Identifier Attributes 8 determines the extent to which the variable can be shared by different parts of the program external linkage may be shared by several (or all) files in the program internal linkage restricted to a single file, but shared by all functions within that file no linkage restricted to a single function

External Linkage external linkage may be shared by several (or all) files in the program Identifier Attributes 9 int numcallstosort = 0; void Sort(int list[], int Sz) { declared outside all blocks can be referred to from other files potentially very dangerous use only if necessary

Internal Linkage Identifier Attributes 10 internal linkage restricted to a single file, but shared by all functions within that file static int numcallstosort = 0; void Sort(int list[], int Sz) { declared outside all blocks, using reserved word static cannot be referred to from other files potentially dangerous use only if necessary

No Linkage Identifier Attributes 11 no linkage restricted to a single function void Sort(int list[], int Sz) { static int numswaps = 0; int startidx = 0; declared inside a block can only be referred to within the block where the declaration is placed

Determining the Attributes Identifier Attributes 12 The default storage duration, scope and linkage of a variable depend on the location of its declaration: inside a block automatic storage duration, block scope, no linkage outside any block static storage duration, file scope, external linkage When the defaults are not satisfactory, see: auto static extern register

Example: Declarations/Definitions Identifier Attributes 13 // A.c int externala = 10; static int statica = 20; void A() { // definition: external linkage, // static storage, file scope // definition: internal linkage, // static storage, file scope int locala = 0; // definition: no linkage, static int localstatica = 0; // definition: no linkage, // static storage, block scope // main.c extern int externala; // declaration, not definition int main() {

Example: Full Source Identifier Attributes 14 // A.c #include <stdio.h> int externala = 10; static int statica = 20; void A() { // definition: external linkage, // static storage, file scope // definition: internal linkage, // static storage, file scope printf(" printf(" A(): externala = %d\n", externala); A(): statica = %d\n", statica); int locala = 0; ++locala; printf(" // definition: no linkage, // auto storage, block scope A(): locala = %d\n", locala); static int localstatica = 0; // definition: no linkage, // static storage, block scope ++localstatica; printf(" A(): localstatica = %d\n", localstatica);

Example: Full Source Identifier Attributes 15 // main.c #include <stdio.h> #include "A.h" extern int externala; // declaration, not definition int main() { printf("main(): making first call to A():\n"); A(); printf("main(): resetting externa:\n"); externala = 5; printf("main(): making second call to A():\n"); A(); printf("main(): making third call to A():\n"); A(); // A.h return 0; #ifndef A_H #define A_H void A(); #endif

Example: First Call Identifier Attributes 16 printf("main(): making first call to A():\n"); A(); main(): making first call to A(): A(): externala = 10 A(): statica = 20 A(): locala = 1 A(): localstatica = 1

Example: Access and Second Call Identifier Attributes 17 printf("main(): resetting externa:\n"); externala = 5; printf("main(): making second call to A():\n"); A(); main(): making second call to A(): A(): externala = 5 A(): statica = 20 A(): locala = 1 A(): localstatica = 2

Example: Third Call Identifier Attributes 18 printf("main(): making third call to A():\n"); A(); main(): making third call to A(): A(): externala = 5 A(): statica = 20 A(): locala = 1 A(): localstatica = 3