Binghamton University. CS-211 Fall Variable Scope

Similar documents
Binghamton University. CS-211 Fall Variable Scope

Binghamton University. CS-211 Fall Variable Scope

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 14

Course organization. Course introduction ( Week 1)

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

CSE 303: Concepts and Tools for Software Development

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

PROGRAMMAZIONE I A.A. 2017/2018

Communication With the Outside World

Pointers and scanf() Steven R. Bagley

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Project 1: How to Make One Dollar

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

Problem Solving and 'C' Programming

Chapter 11 Introduction to Programming in C

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming

Binghamton University. CS-211 Fall Functions. The Basis of C

CSCI565 Compiler Design

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

Chapter 11 Introduction to Programming in C

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

Chapter 11 Introduction to Programming in C

CS 0449 Sample Midterm

Arrays and Pointers (part 2) Be extra careful with pointers!

Arrays and Pointers (part 2) Be extra careful with pointers!

Lecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?

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

Arrays and Strings. Antonio Carzaniga. February 23, Faculty of Informatics Università della Svizzera italiana Antonio Carzaniga

Tutorial 1 C Tutorial: Pointers, Strings, Exec

Procedures, Parameters, Values and Variables. Steven R. Bagley

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

CS 223: Data Structures and Programming Techniques. Exam 2

Today s Learning Objectives

CSC258: Computer Organization. Functions and the Compiler Tool Chain

Bristol Institute of Technology

Array Initialization

Programming in C. What is C?... What is C?

Iterative Languages. Scoping

C: How to Program. Week /Mar/05

Understanding Pointers

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

Why C++ is much more fun than C (C++ FAQ)?

Chapter 11 Introduction to Programming in C

COMP2521. Generic ADTs in C

Fundamentals of Programming Session 12

Chapter 11 Introduction to Programming in C

Arrays and Pointers. CSE 2031 Fall November 11, 2013

CS333 Intro to Operating Systems. Jonathan Walpole

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

A Fast Review of C Essentials Part II

Program Organization and Comments

Chapter 2 - Introduction to C Programming

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

CS107, Lecture 6 More Pointers and Arrays

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

Programming and Data Structure

Worksheet 11 Multi Dimensional Array and String

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Memory Allocation in C

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

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Unit 7. Functions. Need of User Defined Functions

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

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

CS240: Programming in C

Slide Set 9. for ENCM 335 in Fall Steve Norman, PhD, PEng

Object-Oriented Programming, Iouliia Skliarova

Chapter 11 Introduction to Programming in C

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CSE2301. Arrays and Pointers. These slides are based on slides by Prof. Wolfgang Stuerzlinger at York University. Arrays

CSE2301. Arrays. Arrays. Arrays and Pointers

Chapter 13. Functions and Parameter Passing (Part 2)

COMP s1 Lecture 1

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Dynamic memory allocation

primitive arrays v. vectors (1)

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

York University Faculty Science and Engineering Fall 2008

CpSc 1010, Fall 2014 Lab 10: Command-Line Parameters (Week of 10/27/2014)

Pointers. Pointer References

CSE 374 Programming Concepts & Tools

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

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

CS61C : Machine Structures

Lecture 2, September 4

03 Remote invocation. Request-reply RPC. Coulouris 5 Birrel_Nelson_84.pdf RMI

SFU CMPT Topic: Control Statements

QUIZ. What is wrong with this code that uses default arguments?

CSC111 Computer Science II

C programming for beginners

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

CS Operating Systems Lab 3: UNIX Processes

Intermediate Programming, Spring 2017*

Binghamton University. CS-211 Fall Pointers

C programming basics T3-1 -

Topics Introduction to Microprocessors

Programming in C Lecture Tiina Niklander

Transcription:

Variable Scope 1

2

Scope The places in your code that can read and/or write a variable. Scope starts at the location where you declare the variable There may be holes in the scope! Scope ends at the end of the block in which the declare occurs Usually, the function in which the variable was declared 3

Simple Example Block in which j is declared #include <stdio.h> int main(int argc, char **argv) { int j; for(j=0; j<argc; j++) { printf( Argument %d = %s\n,j,argv[j]); return 0; Scope of j 4

Block doesn t have to be a function! You can define a variable inside a sub-block of a function 5

Internal Example Block in which k is declared #include <stdio.h> int main(int argc, char **argv) { int j; for(j=0; j<argc; j++) { int k=j+1; printf( Argument %d = %s\n,k,argv[j]); return 0; Scope of k 6

Global Variable Declared outside of a block Scope is from declaration to the end of the C file! 7

Global Example Block in which nc is declared #include <stdio.h> int nc=0; int myfunc(int n) { nc++; return n; int main(int argc, char **argv) { int j; for(j=0; j<argc; j++) myfunc(2); printf( myfunc called %d times\n,nc); return 0; Scope of nc 8

Global Variable Pros & Cons Advantages Simple and intuitive Enables functions to communicate data with each other Remembers between function calls as well as within function calls Disadvantages Increases the outside information a function needs to be aware of (binding) Prevents re-use of functions Remembers between function calls as well as within function calls 9

Holes in Scopes If the same variable is declared inside a sub-block, the internal declaration temporarily replaces the external definition! 10

Example Scope Hole { int i; int j=7; for (i=0; i<3; i++) { int j=i+1; printf( j=%d\n,j); printf( j=%d\n ); Scope of outside j Scope of inside j (Hole for outside j) j=1 j=2 j=3 j=7 11

Variable Class Automatic Created/Initialized on entry to block Destroyed on exit from block Static Created/Initialized when program starts Destroyed when program ends 12

Default Class Function/Block Variables are automatic Created/Initialized on entry to that function/block Deleted when that function/block ends Global Variables are Static Created/Initialized when the program starts Deleted when the program ends Automatic has no meaning! 13

Local Static Variables We can specify static keyword in a declaration Implies variable is created when program starts, deleted when program ends Does NOT mean that the scope is global!!!! Scope is still within that function 14

Example Local Static char * flipflop() { static int flip=1; if (flip) { flip=0; return flip ; else { flip=1; return flop ; for(i=0;i<8;i++) printf( %s,flipflop()); flip flop flip flop flip flop flip flop 15

BAD FORM : Pseudo-Globals It is legal in C to nest a function inside another function This allows the variables in the outside function to be visible (in scope) for the inside function C Coders frown on this practice! Nested functions have other complications Nested functions cannot be re-used It s ugly and confusing 16

Example Nested Functions int main(int argc, char **argv) { char firstargletter(int i) { return argv[i][0]; int j; for(j=0;j<argc;j++) printf( Arg start: %c\n, firstargletter(j)); return 0; 17

Resources Programming in C, Chapter 7 Wikipedia Variable https://en.wikipedia.org/wiki/variable_(computer_science) Scope Tutorial http://www.tutorialspoint.com/cprogramming/c_scope_rules.htm 18