Lecture 19 CSE August You taught me Language, and my profit on t is I know how to curse. William Shakspere, The Tempest, I, ii.

Similar documents
Lecture 4 CSE July 1992

Lecture 16 CSE July 1992

CSE 374 Programming Concepts & Tools

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

C++ for Java Programmers

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.

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

4. Java Project Design, Input Methods

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

CSE 143 Lecture 10. Recursion

Conditionals: More Fun with If-Then and Logic Operators

Pointers, Arrays and Parameters

Macros in sbt: Problem solved!

CSE332: Data Abstractions Lecture 22: Shared-Memory Concurrency and Mutual Exclusion. Tyler Robison Summer 2010

Reliable programming

CS1102: Macros and Recursion

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

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

Ch. 12: Operator Overloading

Singly linked lists in C.

CSE 230 Intermediate Programming in C and C++ Functions

Lecture 1: Overview

QUIZ Friends class Y;

CS201 Some Important Definitions

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #17. Loops: Break Statement

These are notes for the third lecture; if statements and loops.

Smaller, simpler, subcomponent of program Provides abstraction

Post Experiment Interview Questions

C Pointers 2013 Author Riko H i

CSE 303: Concepts and Tools for Software Development

Intro. Scheme Basics. scm> 5 5. scm>

Functions and Decomposition

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

Assignment 1c: Compiler organization and backend programming

User Defined Functions

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

SEER AKADEMI LINUX PROGRAMMING AND SCRIPTINGPERL 7

If you don t, it will return the same thing as == But this may not be what you want... Several different kinds of equality to consider:

High Performance Computing

CS 430 Computer Architecture. C/Assembler Arithmetic and Memory Access William J. Taffe. David Patterson

Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172. In compensation, no class on Friday, Jan. 31.

CS61C : Machine Structures

A student was asked to point out interface elements in this code: Answer: cout. What is wrong?

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

The Dynamic Typing Interlude

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

Functions, Pointers, and the Basics of C++ Classes

CS125 : Introduction to Computer Science. Lecture Notes #11 Procedural Composition and Abstraction. c 2005, 2004 Jason Zych

Excel Basics: Working with Spreadsheets

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

Welcome to... CS113: Introduction to C

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

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

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

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

COSC 2P95. Introduction. Week 1. Brock University. Brock University (Week 1) Introduction 1 / 18

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

Clickbank Domination Presents. A case study by Devin Zander. A look into how absolutely easy internet marketing is. Money Mindset Page 1

Starting to Program in C++ (Basics & I/O)

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

CS Introduction to Data Structures How to Parse Arithmetic Expressions

THE EVALUATION OF OPERANDS AND ITS PROBLEMS IN C++

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

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

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Data Structure Series

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

Autumn 2012 November 7 th 9 th, 2012 Section Solution

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

LOOPS. Repetition using the while statement

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Dynamic Memory Allocation

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output

(Refer Slide Time: 1:40)

Remaining Enhanced Labs

CMPSCI 187: Programming With Data Structures. Lecture #23: Linked Lists David Mix Barrington 31 October 2012

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Programming Lecture 4

Lecture 04 FUNCTIONS AND ARRAYS

"SQL Monitor now makes the team look more professional."

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

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

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

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

8 Understanding Subtyping

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14

Keep Track of Your Passwords Easily

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

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Lecture 11. Demonstration #1. More Inheritance, More Constructors. Absolute C++ Chapter 15. Virtual Functions. Overriding

DCS/100: Procedural Programming

Programming for Engineers Iteration

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

CS61C : Machine Structures

Transcription:

Lecture 19 CSE 110 5 August 1992 You taught me Language, and my profit on t is I know how to curse. William Shakspere, The Tempest, I, ii. 1 Left-Over Language Features Today was the day we saw the last of the language features, things that are rarely or never important. Each of these I expected to bring up when the appropriate time came, at some moment when it would be useful, and the moment never came. We ll see these features in increasing order of usefulness. 1.1 The, Operator The, operator takes two operands, which must be expressions. To evaluate an expression of the form expr1, expr2, the computer evaluates expr1, and then evaluates expr2. The value of the entire expression is the value of expr2. The, operator has two interesting properties: It guarantees that expr1 will be evaluated first 1, and it guarantees a sequence point between the evaluation of the two expressions. This means that any side effects such as increments or assignments that are scheduled as a result of evaluating expr1 will be completely resolved before expr2 is evaluated. Nevertheless, the, operator is mostly used when you want to stick two expressions where only one normally fits, such as in the condition of a while first. 1 Most operators, such as +, do not guarantee which of their operands will be evaluated 1

CSE 110 Lecture Notes Mark Jason Dominus 2 statement. The example that Kernighan and Ritchie give for the, operator is a program to reverse a string in place, which we ve already seen: (They called it reverse.) void strrev(char *s) { int c, i, j; for (i=0, j=strlen(s)-1; i<j; i++, j++) { c = s[i]; s[i] = s[j]; s[j] = c; The commas that separate a function s arguments, the variables in a declaration, and soforth, are not, operators. 1.2 The?: Operator The?: is unusual in that it has three operands. To evaluate the expression expr1? expr2 : expr3, the computer first evaluates expr1. If expr1 is true, the computer evaluates expr2 and that value is the value of the entire expression. Otherwise, the computer evaluates expr3 and that value is the value of the entire expression. A?: expression is like a miniature if then. Here s an example: This function prints out the elements of an array, separated by spaces, with ten elements per line. void print_array(int *a, int nelems) { int i; for (i=0; i<nelems; i++) printf("%d%c", a[i], (i+1)%10? : \n ); Here s a simpler example: It sets the value of a to the value of either x or y, whichever is smaller: a = (x > y)? y : x ;

CSE 110 Lecture Notes Mark Jason Dominus 3 I swiped this example from K&R also. 1.3 The switch Statement A switch statement has this form: switch (expression) { case value1 : statement11 statement12 case value2 : statement21 statement22 default: statementd1 statementd2 To execute a switch statement, the computer evaluates the expression. Control then passes to the statement immediately after one of the three following things, which are listed in order of preference: 1. A case label in the statement, which has the form case constant : where the value of the constant is the same as the value of the expression. If there is no case label whose value matches that of the expression, control passes to the statement immediately after: 2. A default label in the statement, which has the form default :. If there is no default label, control passes to the statement immediately after: 3. The end of the case statement.

CSE 110 Lecture Notes Mark Jason Dominus 4 Once into the compound statement part of the switch, control flows from top to bottom as usual. Control does not leave the compound statement except by flowing off the bottom of the statement or by encountering a break or return as usual. In particular, after the computer executes the last statement in one of the case sections, it continues on to the statements under the next case section. This usually isn t what we want, and so we almost always end each case section with a break statement. It s so rare to actually allow control to fall through from one case section to the next that you should always put in a comment when you do let control fall through. case can always be replaced by if else if else, but the restrictions on it allow the compiler to generate more efficient code, particularly if there are a lot of case labels. 1.4 Block-Scoped variables We ve only seen variables declared at the head of a function. These variables had names that were known only inside the function in which they were declared, and the variables were created each time the function was called and destroyed again when the function returned. In fact, you can declare new variables at the head of any compound statement. The variables are created when control enters the statement, and destroyed again when control leaves the statement. The names of the variables are known only within the statement. It s good to declare variables as belonging to as small a compound statement as possible, because it keeps the definition and use of the variable close together, because it restricts communication, and because it s easier to understand when you re reading the code you know you can forget about the variable s value unless you re reading the code for the compound statement in which it s declared. We ve actually seen this before: I used it in the sample solution to the guessing game project. 1.5 true and false We ve been saying all semester that false was a zero value and that true was a nonzero value. That s only part of the story.

CSE 110 Lecture Notes Mark Jason Dominus 5 In any condition context, such as the condition part of a while or for loop, or the first operand of a?: expression, the following values mean true and false: Type false true number 0 non-zero pointer NULL not NULL character \0 anything else This means, in particular, that all those times we wrote if (c!= \0 ) we could have just written if (c), and whenever we wrote if (newnode!= NULL) we could just have written if (newnode) instead. Opinion seems to be divided on whether or not this is bad style. Since opinion is divided, and about half the world does in fact use the short forms, it is therefore good style. 2 The Compiler and the Linker The compiler is really two programs in one. The first part, the compiler, translates the C into whatever machine language is appropriate for the machine you re using. The translated version of the program is called an object file. The second part of the compiler, the linker, does a simpler job. It looks through the object file for function calls, and it arranges that the right functions are called. When it s done, it might discover that you called some functions for which you didn t provide code, such as printf; these are called unresolved references. The linker then looks through library files, which contain pre-compiled functions, for functions whose names match the unresolved references. If it finds any, it includes the object code for those functions into your program. If there are still unresolved references, the linker complains that it couldn t finish compiling your program; otherwise it has resolved all the references and creates an executable program. Having a linker means that you can write your program in several different files, compile them separately, months apart, have functions in one file call functions in another, and the linker will sort out all the calls at the end. If you change one function, you only need to recompile the file it s in and then re-link the program; you don t need to recompile the unchanged source files. We didn t see how to do this because we never wrote a program long enough to deserve it.

CSE 110 Lecture Notes Mark Jason Dominus 6 3 Idle Questions We had a lot of idle questions about what object-oriented means; how long it takes to write a big program and how big one is; how one organizes a big programming project, features of other languages, how widespread various languages are, and soforth. I can t possibly remember all the nonsense we discussed, but I do remember that there was one point I wanted to make that I didn t get a chance to bring up: