Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Similar documents
Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Contents. Slide Set 1. About these slides. Outline of Slide Set 1. Typographical conventions: Italics. Typographical conventions. About these slides

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 1 (corrected)

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

ENCM 339 Fall 2017: Editing and Running Programs in the Lab

Slide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

ENCM 335 Fall 2018 Lab 2 for the Week of September 24

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Slide Set 4. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

Slide Set 1. for ENEL 339 Fall 2014 Lecture Section 02. Steve Norman, PhD, PEng

Slide Set 9. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

Contents. Slide Set 2. Outline of Slide Set 2. More about Pseudoinstructions. Avoid using pseudoinstructions in ENCM 369 labs

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

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 1. for ENEL 353 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

ENCM 335 Fall 2018 Tutorial for Week 13

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

Slide Set 4. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Slide Set 3. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

ENCM 339 Fall 2017: Cygwin Setup Help

Slide Set 5. for ENCM 369 Winter 2014 Lecture Section 01. Steve Norman, PhD, PEng

ENCM 369 Winter 2017 Lab 3 for the Week of January 30

ENCM 339 Fall 2017 Tutorial for Week 8

ENCM 501 Winter 2015 Tutorial for Week 5

Integer Multiplication and Division

ENCM 501 Winter 2018 Assignment 2 for the Week of January 22 (with corrections)

ENCM 335 Fall 2018 Lab 6 for the Week of October 22 Complete Instructions

Slide Set 11. for ENCM 369 Winter 2015 Lecture Section 01. Steve Norman, PhD, PEng

Computer Programming: Skills & Concepts (CP) Variables and ints

Welcome to... CS113: Introduction to C

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Slide Set 18. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Slide Set 5. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

Slide Set 18. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

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

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

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

Fundamentals of Programming Session 4

CSE 374 Programming Concepts & Tools

Slides for Lecture 15

ENCM 501 Winter 2016 Assignment 1 for the Week of January 25

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

University of Calgary Department of Electrical and Computer Engineering ENCM 335 Instructor: Steve Norman

University of Calgary Department of Electrical and Computer Engineering ENCM 339 Lecture Section 01 Instructor: Steve Norman

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

T02 Tutorial Slides for Week 2

ENCM 339 Fall 2017 Lecture Section 01 Lab 5 for the Week of October 16

Contents Slide Set 9. Final Notes on Textbook Chapter 7. Outline of Slide Set 9. More about skipped sections in Chapter 7. Outline of Slide Set 9

Slide Set 15 (Complete)

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

Slide Set 5. for ENCM 501 in Winter Term, Steve Norman, PhD, PEng

ENCM 339 Fall 2017 Lecture Section 01 Lab 9 for the Week of November 20

Tutorial 5. Call Stack and Stack Frames. Memory Addresses and Pointers. Content vs Address of Pointers. scanf() function. Perils of Pointers

Chapter 7 Functions. Now consider a more advanced example:

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.

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

Deep C (and C++) by Olve Maudal

Lecture 4 CSE July 1992

CS113: Lecture 4. Topics: Functions. Function Activation Records

Slides for Lecture 15

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

Slide Set 8. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

BSM540 Basics of C Language

CS 137 Part 2. Loops, Functions, Recursion, Arrays. September 22nd, 2017

COMP s1 Lecture 1

ENCM 339 Fall 2017 Lecture Section 01 Lab 3 for the Week of October 2

Goals of this Lecture

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

ENCM 369 Winter 2019 Lab 6 for the Week of February 25

Government Polytechnic Muzaffarpur.

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

Slides for Lecture 6

CpSc 1111 Lab 4 Formatting and Flow Control

Fundamentals of Programming & Procedural Programming

Sums. Here's a (poor) program to add three numbers

CpSc 1111 Lab 5 Formatting and Flow Control

Introduction: The Unix shell and C programming

ENCM 369 Winter 2015 Lab 6 for the Week of March 2

Fundamentals of Programming. Lecture 3: Introduction to C Programming

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

BLM2031 Structured Programming. Zeyneb KURT

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Functions! Objectives! 1E3! Topic 9! programming! n This topic should allow students to! n Read chapter 6 of the textbook now.!

printf( Please enter another number: ); scanf( %d, &num2);

Deep C by Olve Maudal

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Fundamentals of Programming Session 15

UNIVERSITY OF WINDSOR Winter 2007 QUIZ # 1 Solution. Examiner:Ritu Chaturvedi Dated : Feb 7 th, Student Name: Student Number:

ENCM511 Assignment 1 Version 16 September 2012

APS105. Modularity. C pre-defined functions 11/5/2013. Functions. Functions (and Pointers) main. Modularity. Math functions. Benefits of modularity:

ENCM 501 Winter 2017 Assignment 3 for the Week of January 30

CSE 5A Introduction to Programming I (C) Homework 4

Slide Set 5. for ENEL 353 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Transcription:

Slide Set 1 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2016

ENCM 339 Fall 2016 Slide Set 1 slide 2/43 Contents About these slides Writing and running a very simple C program The return value from main Input and output of int values with scanf and printf C code organization for the first few weeks of 339 Activation Records and the Stack Syntax for C function definitions True and False in C

ENCM 339 Fall 2016 Slide Set 1 slide 3/43 Outline of Slide Set 1 About these slides Writing and running a very simple C program The return value from main Input and output of int values with scanf and printf C code organization for the first few weeks of 339 Activation Records and the Stack Syntax for C function definitions True and False in C

ENCM 339 Fall 2016 Slide Set 1 slide 4/43 About these slides This is the first of around ten to twelve large sets of slides that will be used for lectures in Section 01 of ENCM 339 in Fall 2016. It will usually take several lectures to get through a single set of slides. For example, I expect that it will take about three lectures to get through this first set. Reading these slides online is not a good substitute for attending lectures in most lectures I will do some important hand-written work using the document camera. Please come to lectures prepared to take some notes.

ENCM 339 Fall 2016 Slide Set 1 slide 5/43 Typographical conventions Either bold text or bright red text will be used for emphasis. The typewriter font will usually be used for code in C or C++. (I might not use the typewriter font for code if it makes the code too wide to fit in a slide.) Text in a box is a general description of what could appear within a piece of code. Example: A C do statement has this syntax... do statement while ( expression ); (Usually statement is a compound statement that starts with { and ends with }.)

ENCM 339 Fall 2016 Slide Set 1 slide 6/43 Typographical conventions: Italics Italics will be used two different ways. One word or a few words in italics will be used to formally or informally define a term. Example: A loop is a part of a program that allows repeated execution of a list of statements. An entire sentence in italics indicates a pause to elaborate a concept or solve a problem under the document camera. Example: Let s rewrite the for loop as a while loop.

ENCM 339 Fall 2016 Slide Set 1 slide 7/43 Outline of Slide Set 1 About these slides Writing and running a very simple C program The return value from main Input and output of int values with scanf and printf C code organization for the first few weeks of 339 Activation Records and the Stack Syntax for C function definitions True and False in C

ENCM 339 Fall 2016 Slide Set 1 slide 8/43 Writing and running a very simple C program Let s pretend that we have no idea what 2 + 3 and 4 + 7 are, that we need to know those values, and that the only way we can find out is to write a C program that will give us the answers. That s obviously silly! But it s a quick way to create a tiny example programming problem. The next slide has C source code to solve the problem. Let s make some notes about the source code.

ENCM 339 Fall 2016 Slide Set 1 slide 9/43 #include <stdio.h> // note 3 int add(int left, int right); // note 1 int main(void) { int sum; sum = add(2, 3); // note 2 printf("the first sum is %d.\n", sum); sum = add(4, 7); // also note 2 printf("the second sum is %d.\n", sum); return 0; } int add(int left, int right) // note 4 { return left + right; }

ENCM 339 Fall 2016 Slide Set 1 slide 10/43 Source code and executable files Source code for a program is code written by one or more humans, in a programming language such as C. Early in ENCM 339, the source code for a C program will be a single file, with a.c extension at the end of its name. (Later, we ll learn how to organize source code in multiple files.) An executable file contains machine code: lists of instructions in a language that a computer processor chip can understand.

ENCM 339 Fall 2016 Slide Set 1 slide 11/43 Pay attention to which folder you re working in! It s important to make sure that these two things are the same... the folder you save your C source code; the current working directory of the terminal window you use to build and run the executable file. (Note that directory is a synonym for what most Windows and Mac users would call a folder.) Please see the Lab 1 instructions and related documents for details on how to change the current working directory in terminal window.

ENCM 339 Fall 2016 Slide Set 1 slide 12/43 Editing source code, building and running an executable The next few slides show to create a source file for our example C program, then how to build an executable and run it. The very next slide shows a text editor window, after the C code has been typed in and saved to a file called add.c.

ENCM 339 Fall 2016 Slide Set 1 slide 14/43 Below is a screenshot of a Cygwin terminal window. Cygwin is the environment we ll use in the lab for building and testing executables. Let s make some notes about what has happened so far in this example terminal window.

ENCM 339 Fall 2016 Slide Set 1 slide 15/43 The screenshot here is a continuation of the example on the previous slide. What new thing has just happened?

ENCM 339 Fall 2016 Slide Set 1 slide 16/43 Outline of Slide Set 1 About these slides Writing and running a very simple C program The return value from main Input and output of int values with scanf and printf C code organization for the first few weeks of 339 Activation Records and the Stack Syntax for C function definitions True and False in C

ENCM 339 Fall 2016 Slide Set 1 slide 17/43 The return value from main This is a minor detail, but I ve found that if I don t discuss it, I get questions about it. In both standard C and standard C++, main is a function that returns an int value. (Some C and C++ systems allow a return type of void for main, but that s non-standard, so we won t do that in ENCM 339.) The return value from main is used to communicate information from a program back to the operating system that launched the program: returning 0 means the program succeeded ; return a non-zero value says the program failed.

ENCM 339 Fall 2016 Slide Set 1 slide 18/43 Outline of Slide Set 1 About these slides Writing and running a very simple C program The return value from main Input and output of int values with scanf and printf C code organization for the first few weeks of 339 Activation Records and the Stack Syntax for C function definitions True and False in C

ENCM 339 Fall 2016 Slide Set 1 slide 19/43 Input and output of int values with scanf and printf The first few lectures in ENCM 339 are designed to teach you all the C features you need to do Lab 1. So we ll jump quickly through some key features of C. We ll return to some of these features later in the course, and examine them in much greater depth. Let s look first at these two things: getting input of int values from the terminal, using the scanf function; sending output of int values to the terminal, using the printf function. The next slide has a simple example program.

ENCM 339 Fall 2016 Slide Set 1 slide 20/43 #include <stdio.h> int main(void) { int a, b; printf("please enter an int:\n"); scanf("%d", &a); printf("please enter another int:\n"); scanf("%d", &b); printf("%d plus %d is %d.\n", a, b, a + b); return 0; } Let s copy the program so we can think about all of its pieces. Then let s write out an example dialogue with the program.

ENCM 339 Fall 2016 Slide Set 1 slide 21/43 Questions about the use of scanf scanf("%d", &a); 1. What does %d mean? Are there other uses of % for other types of input data? 2. Why is & necessary in front of a? 3. What happens if the user types letters or punctuation instead of digits? 4. Is there is a way to check for input errors?

ENCM 339 Fall 2016 Slide Set 1 slide 22/43 Notes on the call to printf printf("%d plus %d is %d.\n", a, b, a + b); What s going on here? We had to use &, the address-of operator, in the scanf call. Why don t we use it in the printf call?

ENCM 339 Fall 2016 Slide Set 1 slide 23/43 Outline of Slide Set 1 About these slides Writing and running a very simple C program The return value from main Input and output of int values with scanf and printf C code organization for the first few weeks of 339 Activation Records and the Stack Syntax for C function definitions True and False in C

ENCM 339 Fall 2016 Slide Set 1 slide 24/43 C code organization for the first few weeks of 339 Most real world C programs are written as a collection of.c and.h files. But we ll start with programs written in single.c files that have this layout: #include directives function prototypes definition of main definitions of other functions There s an example on the next slide. Let s copy the example and make some notes about it.

#include <stdio.h> int larger(int a, int b); int main(void) { int x = 3, y = 7, z; z = larger(x, y); printf("the larger of %d and %d is %d.\n", x, y, z); return 0; } int larger(int a, int b) { int result = a; if (b > a) result = b; return result; }

ENCM 339 Fall 2016 Slide Set 1 slide 26/43 Outline of Slide Set 1 About these slides Writing and running a very simple C program The return value from main Input and output of int values with scanf and printf C code organization for the first few weeks of 339 Activation Records and the Stack Syntax for C function definitions True and False in C

ENCM 339 Fall 2016 Slide Set 1 slide 27/43 Activation Records and the Stack This is a big ENCM 339 topic that is not covered in detail in textbooks. Careful attention to lectures and to documents posted on D2L is essential. What we re about to study is a model for how C and C++ functions manage their local variables and function arguments. This model will be very helpful in understanding how a wide range of C and C++ features work.

ENCM 339 Fall 2016 Slide Set 1 slide 28/43 What does it mean for a function to be active? This is a important definition: A function is active if its code is currently being executed or if it is waiting for a function call to return. Examples: Let s decide which functions are active at points 1, 2, and 3 in our most recent example program.

ENCM 339 Fall 2016 Slide Set 1 slide 29/43 Activation records Each active function has an activation record ( AR ) in which arguments and local variables are stored. An AR is created automatically when a function becomes active; disappears when the function stops being active. ARs are stored in a region of program memory called the stack (or, sometimes, the function call stack).

ENCM 339 Fall 2016 Slide Set 1 slide 30/43 Memory diagrams A major activity in 339 will be drawing diagrams to illustrate memory use by C and C++ programs. Please use the course conventions for memory diagrams that will be introduced as 339 progresses. Let s make memory diagrams for points 1, 2, and 3 in our most recent example program, then make a couple of remarks.

ENCM 339 Fall 2016 Slide Set 1 slide 31/43 Some conventions for drawing memory diagrams We ll start with these rules: start with AR of main at the bottom of the stack; put arguments in the lower part of the AR; put local variables in the upper part of the AR; use?? to indicate uninitialized (and therefore unknown) values. We ll introduce more conventions later in the course.

ENCM 339 Fall 2016 Slide Set 1 slide 32/43 About Models A model is a simplified description of a natural or engineered system. A good model helps to understand and perhaps to predict the behaviour of a system. Storage of all function arguments and local variables within ARs on the stack is a simple and powerful model that helps explain important C features, such as: how arguments get passed from one function to another; how pointers work; how sequences of recursive function calls work. (Pointers are the main topic of Slide Set 2. Recursive functions will be covered toward the end of this course.)

ENCM 339 Fall 2016 Slide Set 1 slide 33/43 But really, not all variables and arguments are on the stack... The AR model is both vague and incorrect about how modern computer processors work with memory circuits when programs are running. That does not mean that the AR model is bad it s just not sophisticated enough to explain everything about the environment a program runs in. In reality some arguments and variables live in place called processor registers, while other arguments and variables really do live on the stack in main memory. We won t worry about those issues in ENCM 339, but we ll study them in detail in ENCM 369.

ENCM 339 Fall 2016 Slide Set 1 slide 34/43 Outline of Slide Set 1 About these slides Writing and running a very simple C program The return value from main Input and output of int values with scanf and printf C code organization for the first few weeks of 339 Activation Records and the Stack Syntax for C function definitions True and False in C

ENCM 339 Fall 2016 Slide Set 1 slide 35/43 Syntax for C function definitions return-type function name ( argument list ) block In C99 1999 standard C and later C versions (and in all versions of C++), the syntax for a block is: { } mix of zero or more variable declarations and statements The C89 syntax for a block is more restrictive: { } zero or more variable declarations zero or more statements

ENCM 339 Fall 2016 Slide Set 1 slide 36/43 Consider this function definition: int f(int a) { int x; x = 3 * a; int y; y = a * a; return x + y; } Why is it valid code in C99 but not in C89? How could it be modified to make it valid C89? In 339, we ll usually write C blocks in the C89 style, because it provides a compact list of all the arguments and local variables a function has.

ENCM 339 Fall 2016 Slide Set 1 slide 37/43 Outline of Slide Set 1 About these slides Writing and running a very simple C program The return value from main Input and output of int values with scanf and printf C code organization for the first few weeks of 339 Activation Records and the Stack Syntax for C function definitions True and False in C

ENCM 339 Fall 2016 Slide Set 1 slide 38/43 True and False in C This is important to know when reading C code: zero stands for false; all non-zero values stand for true! Example: What is the output if foo is called with an argument bar equal to 0? Equal to 1? Equal to 1? Equal to 42? Equal to 4567? #include <stdio.h> void foo(int bar) { if (bar) printf("red\n"); else printf("blue\n"); }

ENCM 339 Fall 2016 Slide Set 1 slide 39/43 From the previous slide: In C, zero stands for false; all non-zero values stand for true! This rule works with many types, not just int. The example function foo would behave the same way if the type of the argument bar were double. The rule also works with pointer types. We ll be looking at pointer types in the next slide set.

ENCM 339 Fall 2016 Slide Set 1 slide 40/43 True and false in Processing Processing does not follow the zero means false, anything non-zero is true rule! Instead, Processing has a type called boolean. A boolean expression has one of two values: true or false. In Processing, a comparison expression like k < 3 would be of type boolean. In C, the same comparison expression would be of type int, and would have a value of 0 or 1, depending on the value of k.

ENCM 339 Fall 2016 Slide Set 1 slide 41/43 Comparing values The C comparison operators are the same as in Processing: ==!= < <= > >= Know the difference between = and ==... What does x = y do? And what does x == y do? Suppose k is an int with a value of 8. Let s fill in the table... expression C type C value Processing type Processing value k == 8 k!= 8 k < 8 k <= 8

ENCM 339 Fall 2016 Slide Set 1 slide 42/43 More examples of numbers being true or false These also serve as review of while loops, and of the difference between i-- and --i. What output is produced by each of these C code fragments? // Example 1 int i = 4; while (i) { printf("%d\n",i); i--; } // Example 2 int i = 4; while (i--) { printf("%d\n",i); } // Example 3 int i = 4; while (--i) { printf("%d\n",i); } Remark: Your instructor likes the style of Example 1 and dislikes Examples 2 and 3. Code is easier to read if testing a variable is separated from modifying the value of that variable.

ENCM 339 Fall 2016 Slide Set 1 slide 43/43 Connections with C++ C++ supports the C rule for representing true and false with numbers. But C++ also has these keywords: bool, a type; true and false, the two values an expression of type bool can have. in C++, a variable for the answer to a yes/no question is usually of type bool. But in C, such a variable would probably be of type int. (In C there is a way to fake the existence of a bool type, using the library header <stdbool.h>, but it doesn t get used much.)