Scope (visibility) scope in C is simple:

Slides:



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

Introduction to C Programming
1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Passing by-value vs. by-reference in ARM by value C code equivalent assembly code int a;.section since a is not assigned an a:.skip initial.
Chapter 7: User-Defined Functions II
16/11/2015 9:05 AM6/11/2015 9:05 AM6/11/2015 9:05 AMFunctions Functions A function consists of: Name Name Arguments (also called parameters) Arguments.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
"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,
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
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.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Storage & Linkage: Effects on Scope Rudra Dutta CSC Spring 2007, Section 001.
Storage Classes.
COMP More About Classes Yi Hong May 22, 2015.
Scope.
Abstract Data Types Using Classes Lecture-5. Abstract Data Types Using Classes Representing abstract data types using C++ We need the following C++ keywords.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Chapter 7 Evaluating the Instruction Set Architecture of H1: Part 1.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
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.
Improving Program Performance Function Visibility in z/TPF C++ Load Modules October 2, /2/20151American Express Public.
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.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
1 Homework / Exam Finish up K&R Chapters 3 & 4 Starting K&R Chapter 5 Next Class HW4 due next class Go over HW3 solutions.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
 2007 Pearson Education, Inc. All rights reserved Random Number Generation  rand function – Load – Returns "random" number between
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.
CSCI 171 Presentation 6 Functions and Variable Scope.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Chapter 13 : Symbol Management in Linking
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.
 constant represented by a name, just like a variable, but whose value cannot be changed  The const keyword precedes the type, name, and initialization.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
1 Classes struct Public and Private Parts of a struct Class Scope of a Class Overloading Member Functions Class in a Class Static Members of Classes this.
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.
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
Chapter 6 Modularity Using Functions
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Advanced Programming in C
Chapter 9: Value-Returning Functions
Data in Memory variables have multiple attributes symbolic name
Suppose we want to print out the word MISSISSIPPI in big letters.
Suppose we want to print out the word MISSISSIPPI in big letters.
Functions and Structured Programming
Programmazione I a.a. 2017/2018.
CS 240 – Lecture 5 Scope of Variables, The Stack, Automatic Variables, Global Variables, Constant Type.
Passing by-value vs. by-reference in ARM
Packages and Interfaces
Local Variables, Global Variables and Variable Scope
Scope Rules and Storage Types
A Simple Two-Pass Assembler
Namespaces How Shall I Name Thee?.
C Programming Getting started Variables Basic C operators Conditionals
Scope.
Compiler vs linker The compiler translates one .c file into a .o file
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Methods/Functions.
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.
SPL – PS1 Introduction to C++.
Scope Rules.
Presentation transcript:

Scope (visibility) scope in C is simple: normal declaration inside a procedure is visible only within that procedure normal declaration outside a procedure is visible to all code below that point in that source file external declaration (extern) informs the linker of a reference to a non-static/non-local variable or function in another source file

Scope (visibility) scope in assembly is similar: unless otherwise specified, symbols are visible only within the source file (thus, you can't use "loop" as a label twice within the same source file, but each separate source file can use "loop" as a local label)

Scope (visibility) scope in assembly is similar: for external references (i.e., between source files), a pseudo-op is used in one source file to indicate the global availability of a symbol defined in that file (i.e., exporting the symbol) and another pseudo-op is used in another source file to indicate use of that symbol (i.e., importing that symbol); up to the linker to match the uses with the definitions

Scope (visibility) In MASM, the two pseudo-ops PUBLIC and EXTERN are used for these purposes In ARM assembly, .global is used both in the exporting file and in the importing file

Scope (visibility) Using MASM, the two pseudo-ops: -- example correspondint C statements -- define(EXTERN, global) define(PUBLIC, global) file a: .EXTERN sqrt file d: extern double sqrt(double); /* usually omit "extern" here */ /* "static" means not public */ .extern x extern int x; /* "referencing" */ /* declaration */ .section ".data" w: .word 0 int w; .section ".text" ... ... bl sqrt z = sqrt (y); ldr r1, =x w = x; ldr r2, [r1] ldr r1, =w ... str r2, [r1]

Scope (visibility) file b: .global sqrt file e: double sqrt(double argument) { sqrt: ... … } ... file c: .global x file f: int x; /* "defining" decl. */ .section ".data" x: .word 0

Scope (visibility) Many C compilers/linkers assume that global variables with the same name in multiple "defining declarations" (i.e., no extern keywords) are identical. The exception is when more than one of these identically-named variables is initialized.

Scope (visibility) -- file global_a_part_1.c -- -- file global_a_part_2.c -- #include<stdio.h> #include<stdio.h> int a; int a; void subr( void ); void subr() { a = 2; int main() { printf("a in subr is %d\n",a); a = 1; } printf("a in main is %d\n",a); subr(); return 0; } gcc global_a_part_1.c global_a_part_2.c ./a.out Output: a in main is 1 a in subr is 2 a in main is 2

Scope (visibility) C has static scoping, but there are languages with dynamic scoping, such as Perl, in which the variable names have to be resolved at run time based on the current nesting level or call chain Consider the examples on the next three slides.

Scope (visibility) in C cat scope_test.c #include<stdio.h> char str[] = "Global!"; void print_it() { printf("%s\n",str); } int main() { { char str[] = "Local!"; print_it();

Scope (visibility) gcc scope_test.c ./a.out Global!

Scope (visibility) in Perl cat scope_test.pl $str = "Global!\n"; sub print_it { print $str; } { local $str = "Local!\n"; print_it();p print_it(); perl scope_test.pl Local! Global! [adapted from www.programmersheaven.com/mb/perl/82538/82538/dynamic-scope/]