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.)