CS 33. Introduction to C. Part 4. CS33 Intro to Computer Systems IV 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Similar documents
CS 33. Introduction to C. Part 3. CS33 Intro to Computer Systems III 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

CS 33. Introduction to C. Part 5. CS33 Intro to Computer Systems V 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Meet & Greet! Come hang out with your TAs and Fellow Students (& eat free insomnia cookies)

Pointers and scanf() Steven R. Bagley

CS 33. Data Representation, Part 2. CS33 Intro to Computer Systems VIII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

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

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Lecture 3: C Programm

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

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Display Input and Output (I/O)

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

CS 33. Data Representation (Part 3) CS33 Intro to Computer Systems VIII 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Fundamental of Programming (C)

Lecture 3. Variables. Variables

Lecture 2: C Programm

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

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

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

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

PROGRAMMAZIONE I A.A. 2017/2018

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

Topic 6: A Quick Intro To C

CS 33. Linkers. CS33 Intro to Computer Systems XXV 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

QUIZ. What are 3 differences between C and C++ const variables?

Procedural programming with C

C PROGRAMMING Lecture 4. 1st semester

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

Constants, References

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

1. In C++, reserved words are the same as predefined identifiers. a. True

Structures. 21 July 2009 Programming and Data Structure 1

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

CS1 Recitation. Week 2

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

What is a Structure? related data items. Examples: n Used for handling a group of logically

CS201 Some Important Definitions

Tutorial 6. Memory Addresses and Pointers. scanf() function. Perils of Pointers. Function Pointers. Declarations vs. Definitions

Chapter 11 Introduction to Programming in C

CS 61C: Great Ideas in Computer Architecture Introduction to C

CS 33. Intro to Storage Allocation. CS33 Intro to Computer Systems XXVI 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

211: Computer Architecture Summer 2016

Writing to and reading from files

Informatik I (D-ITET) Übungsstunde 12,

CMSC 330: Organization of Programming Languages. Lets, Tuples, Records

Introduction to C++ with content from

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

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

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

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

Lectures 5-6: Introduction to C

BASIC ELEMENTS OF A COMPUTER PROGRAM

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

Chapter 11 Introduction to Programming in C

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

CSCI 171 Chapter Outlines

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

1/31/2018. Overview. The C Programming Language Part 2. Basic I/O. Basic I/O. Basic I/O. Conversion Characters. Input/Output Structures and Arrays

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

The C Programming Language Part 2. (with material from Dr. Bin Ren, William & Mary Computer Science)

CS 314 Principles of Programming Languages. Lecture 9

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

Understand Execution of a Program

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

Contents. A Review of C language. Visual C Visual C++ 6.0

M.EC201 Programming language

Operators and Control Flow. CS449 Fall 2017

Syntax Errors; Static Semantics

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

Fundamentals of Programming

The Pyth Language. Administrivia

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

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

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

Fundamental of Programming (C)

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

Ch. 12: Operator Overloading

CS 222: Arrays, Strings, Structs

Kurt Schmidt. October 30, 2018

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

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

Arrays and Pointers in C & C++

Week 2: Syntax Specification, Grammars

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

C: How to Program. Week /Mar/05

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

BLM2031 Structured Programming. Zeyneb KURT

Fundamentals of Programming & Procedural Programming

Basic and Practice in Programming Lab7

More on Functions. Lecture 7 COP 3014 Spring February 11, 2018

CS 241 Data Organization Pointers and Arrays

Input/Output Week 5:Lesson 16.1

Transcription:

CS 33 Introduction to C Part 4 CS33 Intro to Computer Systems IV 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Lifetime int count; int main() { func();... func(); // what s printed by func? return 0; int func() { int a; if (count == 0) a = 1; count = count + 1; printf("%d\n", a); return 0; %./a.out 1-38762173 CS33 Intro to Computer Systems IV 2 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Lifetime (continued) int main() { func(1); // what s printed by func? int a; return 0; int func(int x) { if (x == 1) { a = 1; else func(2); printf("%d\n", a); a = 2; return 0; %./a.out 2 CS33 Intro to Computer Systems IV 3 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Lifetime (still continued) int main() { func(1); // what s printed by func? return 0; int func(int x) { int a; if (x == 1) { else a = 1; func(2); printf("a = %d\n", a); a = 2; return 0; %./a.out 1 CS33 Intro to Computer Systems IV 4 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Lifetime (more...) int main() { int *a; a = func(); printf("%d\n", *a); // what s printed? return 0; int *func() { int x; x = 1; return &x; %./a.out 23095689 CS33 Intro to Computer Systems IV 5 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Lifetime (and still more...) int main() { int *a; a = func(1); printf("%d\n", *a); // what s printed? return 0; int *func(int x) { return &x; %./a.out 98378932 CS33 Intro to Computer Systems IV 6 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Rules Global variables exist for the duration of program s lifetime Local variables and arguments exist for the duration of the execution of the procedure from call to return each execution of a procedure results in a new instance of its arguments and local variables CS33 Intro to Computer Systems IV 7 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Implementation: Stacks int main() { int a; func1(0);... int func1(int x) { int a,b; if (x==0) func2(a,2);... int func2(int x, int y) { int a,b,c; func1(1);... main s stack frame func1 s stack frame func2 s stack frame func1 s stack frame a arg x a, b arg x,y a, b, c arg x a, b CS33 Intro to Computer Systems IV 8 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Implementation: Stacks int main() { int a; func1(0);... int func1(int x) { int a,b; if (x==0) func2(a,2);... int func2(int x, int y) { int a,b,c; func1(1);... main s stack frame func1 s stack frame func2 s stack frame func1 s stack frame a arg x a, b arg x,y a, b, c arg x a, b CS33 Intro to Computer Systems IV 9 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Quiz 1 void proc(int a) { int b=1; if (a == 1) { proc(2); printf("%d\n", b); else { int main() { proc(1); b = a*(b++)*b; return 0; What s printed? a) 0 b) 1 c) 2 d) 4 CS33 Intro to Computer Systems IV 10 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Static Local Variables int *sub1() { int var = 1; return &var; /* amazingly illegal */ int *sub2() { static int var = 1; return &var; /* (amazingly) legal */ Scope like local variables Lifetime like global variables CS33 Intro to Computer Systems IV 11 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Quiz 2 int sub() { static int svar = 1; int lvar = 1; svar += lvar; lvar++; return svar; What is printed? a) 2 b) 3 c) 4 d) 5 int main() { sub(); printf("%d\n", sub()); return 0; CS33 Intro to Computer Systems IV 12 Copyright 2017 Thomas W. Doeppner. All rights reserved.

scanf: Reading Data int main() { int i, j; i scanf("%d %d", &i, f &j); Two parts formatting instructions whitespace in format string matches any amount of white space in input» whitespace is space, tab, newline ( \n ) arguments: must be addresses why? CS33 Intro to Computer Systems IV 13 Copyright 2017 Thomas W. Doeppner. All rights reserved.

#define (again) #define CtoF(cel) (9.0*cel)/5.0 + 32.0 Simple textual substitution: float tempc = 20.0; float tempf = CtoF(tempc); // same as tempf = (9.0*tempc)/5.0 + 32.0; CS33 Intro to Computer Systems IV 14 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Careful... #define CtoF(cel) (9.0*cel)/5.0 + 32.0 float tempc = 20.0; float tempf = CtoF(tempc+10); // same as tempf = (9.0*tempc+10)/5.0 + 32.0; #define CtoF(cel) (9.0*(cel))/5.0 + 32.0 float tempc = 20.0; float tempf = CtoF(tempc+10); // same as tempf = (9.0*(tempc+10))/5.0 + 32.0; CS33 Intro to Computer Systems IV 15 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Structures struct ComplexNumber { float real; ; float imag; struct ComplexNumber x; x.real = 1.4; x.imag = 3.65e-10; CS33 Intro to Computer Systems IV 16 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Pointers to Structures struct ComplexNumber { float real; ; float imag; struct ComplexNumber x, *y; x.real = 1.4; x.imag = 3.65e-10; y = &x; y->real = 2.6523; y->imag = 1.428e20; CS33 Intro to Computer Systems IV 17 Copyright 2017 Thomas W. Doeppner. All rights reserved.

structs and Functions struct ComplexNumber ComplexAdd( struct ComplexNumber a1, struct ComplexNumber a2) { struct ComplexNumber result; result.real = a1.real + a2.real; result.imag = a1.imag + a2.imag; return result; CS33 Intro to Computer Systems IV 18 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Would This Work? struct ComplexNumber *ComplexAdd( struct ComplexNumber *a1, struct ComplexNumber *a2) { struct ComplexNumber result; result.real = a1->real + a2->real; result.imag = a1->imag + a2->imag; return &result; CS33 Intro to Computer Systems IV 19 Copyright 2017 Thomas W. Doeppner. All rights reserved.

How About This? void ComplexAdd( struct ComplexNumber *a1, struct ComplexNumber *a2, struct ComplexNumber *result) { result->real = a1->real + a2->real; result->imag = a1->imag + a2->imag; return; CS33 Intro to Computer Systems IV 20 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Using It struct ComplexNumber j1 = {3.6, 2.125; struct ComplexNumber j2 = {4.32, 3.1416; struct ComplexNumber sum; ComplexAdd(&j1, &j2, &sum); CS33 Intro to Computer Systems IV 21 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Arrays of structs struct ComplexNumber j[10]; j[0].real = 8.127649; j[0].imag = 1.76e18; CS33 Intro to Computer Systems IV 22 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Arrays, Pointers, and structs /* What's this? */ struct ComplexNumber *jp[10]; struct ComplexNumber j0; jp[0] = &j0; jp[0]->real = 13.6; CS33 Intro to Computer Systems IV 23 Copyright 2017 Thomas W. Doeppner. All rights reserved.

jp Memory View 0 1 2 3 4 5 6 7 8 9 j0: 13.6 CS33 Intro to Computer Systems IV 24 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Quiz 3 struct list_elem { int val; struct list_elem *next; a, b; int main() { a->val = 1; a->next = &b; b->val = 2; printf("%d\n", a->next->val); return 0; What happens? a) syntax error b) seg fault c) prints something and terminates CS33 Intro to Computer Systems IV 25 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Quiz 4 struct list_elem { int val; struct list_elem *next; a, b; int main() { a.val = 1; a.next = &b; b.val = 2; printf("%d\n", a.next.val); return 0; What happens? a) syntax error b) seg fault c) prints something and terminates CS33 Intro to Computer Systems IV 26 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Quiz 5 struct list_elem { int val; struct list_elem *next; a, b; int main() { a.val = 1; b.val = 2; printf("%d\n", a.next->val); return 0; What happens? a) syntax error b) seg fault c) prints something and terminates CS33 Intro to Computer Systems IV 27 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Quiz 6 struct list_elem { int val; struct list_elem *next; a, b; int main() { a.val = 1; a.next = &b; b.val = 2; printf("%d\n", a.next->val); return 0; What happens? a) syntax error b) seg fault c) prints something and terminates CS33 Intro to Computer Systems IV 28 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Structures vs. Objects Are structs objects? (What s an object?) CS33 Intro to Computer Systems IV 29 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Structures Containing Arrays struct Array { int A[6]; S1, S2; int A1[6], A2[6]; A1 = A2; // not legal: arrays don t know how big they are S1 = S2; // legal: structures do CS33 Intro to Computer Systems IV 30 Copyright 2017 Thomas W. Doeppner. All rights reserved.

A Bit More Syntax Constants const double pi = 3.141592653589793238; area = pi*r*r; /* legal */ pi = 3.0; /* illegal */ CS33 Intro to Computer Systems IV 31 Copyright 2017 Thomas W. Doeppner. All rights reserved.

More Syntax const int six = 6; int nonconstant; const int *ptr_to_constant; int *const constant_ptr = &nonconstant; const int *const constant_ptr_to_constant = &six; ptr_to_constant = &six; // ok *ptr_to_constant = 7; // not ok *constant_ptr = 7; // ok constant_ptr = &six; // not ok CS33 Intro to Computer Systems IV 32 Copyright 2017 Thomas W. Doeppner. All rights reserved.

And Still More Array initialization int FirstSixPrimes[6] = {2, 3, 5, 7, 11, 13; int SomeMorePrimes[] = {17, 19, 23, 29; int MoreWithRoomForGrowth[10] = {31, 37; int MagicSquare[][] = {{2, 7, 6, {9, 5, 1, {4, 3, 8; CS33 Intro to Computer Systems IV 33 Copyright 2017 Thomas W. Doeppner. All rights reserved.