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

Similar documents
Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Day06 A. Young W. Lim Wed. Young W. Lim Day06 A Wed 1 / 26

Day06 A. Young W. Lim Mon. Young W. Lim Day06 A Mon 1 / 16

Fundamentals of Programming Session 12

Day02 A. Young W. Lim Sat. Young W. Lim Day02 A Sat 1 / 12

Link 3. Symbols. Young W. Lim Mon. Young W. Lim Link 3. Symbols Mon 1 / 42

Fundamentals of Programming Session 13

Function Call Stack and Activation Records

Lecture 04 FUNCTIONS AND ARRAYS

Day14 A. Young W. Lim Thr. Young W. Lim Day14 A Thr 1 / 14

A Fast Review of C Essentials Part II

Accessibility (1A) Young Won Lim 8/22/13

Day14 A. Young W. Lim Tue. Young W. Lim Day14 A Tue 1 / 15

Class (1A) Young Won Lim 9/8/14

Day21 A. Young W. Lim Wed. Young W. Lim Day21 A Wed 1 / 13

Link 7.A Static Linking

Arrays. Young W. Lim Mon. Young W. Lim Arrays Mon 1 / 17

Stack Debugging. Young W. Lim Thr. Young W. Lim Stack Debugging Thr 1 / 12

Stack Tutorial. Young W. Lim Sat. Young W. Lim Stack Tutorial Sat 1 / 15

Link 7. Dynamic Linking

University of Kelaniya Sri Lanka

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

Pointers (1A) Young Won Lim 11/1/17

C Programming Language

GAS Tutorial - 4. Sections & Relocation

Arrays. Young W. Lim Wed. Young W. Lim Arrays Wed 1 / 19

Functions and Recursion

Function Overview (1A) Young Won Lim 10/23/17

GAS Tutorial - 6. Expression

Algorithms Bubble Sort (1B) Young Won Lim 4/5/18

Applications of Pointers (1A) Young Won Lim 12/26/17

Link 8.A Dynamic Linking

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

Functions. (transfer of parameters, returned values, recursion, function pointers).

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Access. Young W. Lim Sat. Young W. Lim Access Sat 1 / 19

Iterative Languages. Scoping

GDB Tutorial. Young W. Lim Thr. Young W. Lim GDB Tutorial Thr 1 / 24

Stack Debugging. Young W. Lim Sat. Young W. Lim Stack Debugging Sat 1 / 40

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

GDB Tutorial. Young W. Lim Fri. Young W. Lim GDB Tutorial Fri 1 / 24

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

HyperGeometric Distribution

Lecture 3. Variables. Variables

Pointers (1A) Young Won Lim 3/5/18

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

Pointers (1A) Young Won Lim 1/9/18

Pointers (1A) Young Won Lim 1/5/18

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

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

Logic Haskell Exercises

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

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

CSE 230 Intermediate Programming in C and C++ Functions

CS240: Programming in C

Lecture 04 FUNCTIONS AND ARRAYS

C Functions. Object created and destroyed within its block auto: default for local variables

Applications of Pointers (1A) Young Won Lim 2/27/18

GDB Tutorial. Young W. Lim Tue. Young W. Lim GDB Tutorial Tue 1 / 32

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

Overview (1A) Young Won Lim 9/14/17

C: How to Program. Week /Apr/16

2. HW/SW Co-design. Young W. Lim Thr. Young W. Lim 2. HW/SW Co-design Thr 1 / 21

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Access. Young W. Lim Fri. Young W. Lim Access Fri 1 / 18

Overview (1A) Young Won Lim 9/9/17

PROGRAMMAZIONE I A.A. 2017/2018

Applications of Pointers (1A) Young Won Lim 3/14/18

Applications of Pointers (1A) Young Won Lim 3/21/18

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

Type (1A) Young Won Lim 2/17/18

The Three Attributes of an Identifier

Unit 7. Functions. Need of User Defined Functions

Overview (1A) Young Won Lim 9/25/17

Applications of Pointers (1A) Young Won Lim 1/5/18

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

Pointers (1A) Young Won Lim 2/6/18

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

Class (1A) Young Won Lim 11/20/14

Applications of Pointers (1A) Young Won Lim 4/11/18

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

Pointers (1A) Young Won Lim 2/10/18

Control. Young W. Lim Mon. Young W. Lim Control Mon 1 / 16

Functions. Systems Programming Concepts

C: How to Program. Week /Mar/05

M.EC201 Programming language

Methods (2A) Young Won Lim 12/10/14

Pointers (1A) Young Won Lim 10/18/17

Programming Languages

15 FUNCTIONS IN C 15.1 INTRODUCTION

GAS Tutorial - 7. Directives (2)

HW / SW Implementation Overview (0A) Young Won Lim 7/16/16

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Applications of Pointers (1A) Young Won Lim 3/31/18

Lecture 3: C Programm

Chapter 7 Functions. Now consider a more advanced example:

Link 2. Object Files

Applications of Pointers (1A) Young Won Lim 4/24/18

Monad Background (3A) Young Won Lim 11/20/17

Pointers (1A) Young Won Lim 10/23/17

Transcription:

Day08 A Young W. Lim 2017-10-16 Mon Young W. Lim Day08 A 2017-10-16 Mon 1 / 27

Outline 1 Based on 2 C Functions (2) Storage Class and Scope Storage Class Specifiers A. Storage Duration B. Scope C. Linkage Young W. Lim Day08 A 2017-10-16 Mon 2 / 27

Based on "C How to Program", Paul Deitel and Harvey Deitel I, the copyright holder of this work, hereby publish it under the following licenses: GNU head Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled GNU Free Documentation License. CC BY SA This file is licensed under the Creative Commons Attribution ShareAlike 3.0 Unported License. In short: you are free to share and make derivative works of the file under the conditions that you appropriately attribute it, and that you distribute it only under a license compatible with this one. Young W. Lim Day08 A 2017-10-16 Mon 3 / 27

Storage Class the storage class specifier auto register extern static an identifier s storage class and scope rules determine storage duration scope linkage Young W. Lim Day08 A 2017-10-16 Mon 4 / 27

Identifier of interests an identifier a variable name a function name Young W. Lim Day08 A 2017-10-16 Mon 5 / 27

Identifier Attributes storage duration (temporal) the period during which an identifier exists in memory scope (spatial) where an identifier can be referenced in a program linkage determines whether an identifier is known only in the current file or in any other file Young W. Lim Day08 A 2017-10-16 Mon 6 / 27

Classification of Identifier Attributes storage duration automatic storage duration static storage duration scope function scope file scope * block scope * function prototype scope linkage external linkage internal linkage Young W. Lim Day08 A 2017-10-16 Mon 7 / 27

Storage Duration Examples #include <stdio.h> void func() { int i = 0; #include <stdio.h> void func() { static int i = 0; } printf("i= %d\n", i); i++; } printf("i= %d\n", i); i++; int main(void) { func(); func(); func(); func(); } --- i= 0; i= 0; i= 0; i= 0; automatic storage class int main(void) { func(); func(); func(); func(); } --- i= 0; i= 1; i= 2; i= 3; static storage class Young W. Lim Day08 A 2017-10-16 Mon 8 / 27

Scope Examples #include <stdio.h> void func() { int i = 111; printf("1.i= %d\n", i); { int j = 222; printf("2.i= %d\n", i); printf("2.j= %d\n", j); { int k = 333; printf("3.i= %d\n", i); printf("3.j= %d\n", j); printf("3.k= %d\n", k); } printf("2.i= %d\n", i); printf("2.j= %d\n", j); } } printf("1.i= %d\n", i); int main(void) { } func(); --- 1.i= 111 2.i= 111 2.j= 222 3.i= 111 3.j= 222 3.k= 333 2.i= 111 2.j= 222 1.i= 111 Young W. Lim Day08 A 2017-10-16 Mon 9 / 27

Linkage Examples #include <stdio.h> int a; void func1(void) { puts("func1 is called"); } void func3(void) ; int main(void) { printf("a= %d\n", a); } func1(); // func2(); func3(); t1.c #include <stdio.h> int a = 333; static void func2(void) { puts("func2 is called"); } void func3(void) { printf("func3: "); func2(); } --- gcc -c t1.c gcc -c t2.c gcc t1.o t2.o t2.c Young W. Lim Day08 A 2017-10-16 Mon 10 / 27

Storage duration storage duration the period during which an identifier exists in memory some exists briefly and are repeatedly created and destroyed (variables defined inside a function) others exists for the program s entire execution (variables defined outside all functions) automatic storage duration (auto + scope) static storage duration (static + scope) Young W. Lim Day08 A 2017-10-16 Mon 11 / 27

Automatic Storage Duration automatic storage duration variables are created when the program control is entered a block {...} where the variable was defined automatic storage duration variables exists while the block is active (while the control is in the block) Young W. Lim Day08 A 2017-10-16 Mon 12 / 27

Static Storage Duration extern and static in the declaration of variables and functions static (storage) variables and static (storage) functions exist from the program starts and until the program ends static (storage) variables are allocated and initialized only once before the program executes extern / static global variables extern / static functions static local variables Young W. Lim Day08 A 2017-10-16 Mon 13 / 27

Storage Class and Linkage functions global variables local variables extern static storage static storage static storage external linkage external linkage * static static storage static storage auto storage internal linkage internal linkage NA Young W. Lim Day08 A 2017-10-16 Mon 14 / 27

Local Variables only variables, not a function can have automatic storage duration all functions and global variables have static storage duration functions s local variable func(int a) { int b, c;... } these variables have automatic storage duration by default func(auto int a) { auto int b, c;... } refered as automatic variables Young W. Lim Day08 A 2017-10-16 Mon 15 / 27

Global Variables global variables variables declared outside all function definitions functions are declared always outside any function definition global variables and functions extern storage class by default any functions can reference global variables and functions these functions must be defined / declared after global variables and functions in the file Young W. Lim Day08 A 2017-10-16 Mon 16 / 27

Static Local Variables local variables with static keyword known only in the function where the local variables are defined retain the values when the function exits (the value is preserved across function calls) can start with the retained value when the function is called again initialized with zero by default when no explicit initialization exists Young W. Lim Day08 A 2017-10-16 Mon 17 / 27

Scope scope where an identifier can be referenced in a program some can be referenced throughout a program (global variable) others from only portions of a program (local variable) Young W. Lim Day08 A 2017-10-16 Mon 18 / 27

Scope Types function scope.............. func(...) {... } file scope.................. t1.c t2.c block scope................. {... } funciton prototype scope.... func(...); Young W. Lim Day08 A 2017-10-16 Mon 19 / 27

Function Scope labels are the only identifiers with function scope start*:* goto switch labels can be used anywhere in the function labels cannot be referenced outside the function body Young W. Lim Day08 A 2017-10-16 Mon 20 / 27

File Scope identifiers declared outside any function known (accessible) in all functions from the point at which the identifier is declared until the end of the file gloabl variables function definitions function prototypes Young W. Lim Day08 A 2017-10-16 Mon 21 / 27

Block Scope identifiers defined inside a block block scope ends at the terminating right brace (}) of the block local variables defined at the beginning of a function function parameter variables also have block scope any block can have its own variable definitions static local variable still have block scope but static storage duration Young W. Lim Day08 A 2017-10-16 Mon 22 / 27

Hiding Block Scope blocks can be nested identifiers of the outer block and inner block can have the same name then the outer identifier is hidden by the inner identifier (higher priority) Young W. Lim Day08 A 2017-10-16 Mon 23 / 27

Function Prototype Scope the only identifer is the parameter list of a function prototype function prototypes require no variable names in the parameter list only types in the parameter list the compiler ignores the variable name the identifers used in a functon prototype can be reused elsewhere in the program Young W. Lim Day08 A 2017-10-16 Mon 24 / 27

Linkage linkage determines for a multiple-source-file program whether an identifier is known only in the current source file or in any source file with proper declarations static prevents an identifier from being referenced in other files static global variables static funtions extern indicates an identifer is defined either later in the same file or in a different file extern global variables extern funtions Young W. Lim Day08 A 2017-10-16 Mon 25 / 27

Internal Linkage it is possible to restrict the scope of a variable or a function to the file in which it is defined can be prevented from being used by any function that are defined in other files static gloabal variables static functions Young W. Lim Day08 A 2017-10-16 Mon 26 / 27

External Linkage non-static gloabal variables non-static functions they can be accesed in other files if those files contain proper declaration and/or function prototypes Young W. Lim Day08 A 2017-10-16 Mon 27 / 27