CS16 Week 2 Part 2. Kyle Dewey. Thursday, July 5, 12

Similar documents
CS16 Exam #1 7/17/ Minutes 100 Points total

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Week 7 Part I. Kyle Dewey. Monday, August 6, 12

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

Lecture 4 CSE July 1992

C: How to Program. Week /Mar/05

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

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

Programming for Engineers Introduction to C

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

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

Chapter 2 - Introduction to C Programming

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

Operators and Expressions:

Full file at C How to Program, 6/e Multiple Choice Test Bank

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

CSCI 171 Chapter Outlines

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

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Basic Types and Formatted I/O

Work relative to other classes

CMSC 104 -Lecture 6 John Y. Park, adapted by C Grasso

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

A Fast Review of C Essentials Part I

ISA 563 : Fundamentals of Systems Programming

Chapter 1 & 2 Introduction to C Language

Data types, variables, constants

Variables and literals

First of all, it is a variable, just like other variables you studied

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Fundamental of Programming (C)

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

Programming in C++ 5. Integral data types

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

CS102: Variables and Expressions

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS110: PROGRAMMING LANGUAGE I

Lecture 2: C Programming Basic

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

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

Data Types and Variables in C language

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

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Chapter 2 Basic Elements of C++

C++ Programming: From Problem Analysis to Program Design, Third Edition

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Week 6 Part 1. Kyle Dewey. Monday, July 30, 12

Computer System and programming in C

Goals of this Lecture

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

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

2. Numbers In, Numbers Out

Computers in Engineering. Moving From Fortran to C Michael A. Hawker

CS 31: Intro to Systems Binary Arithmetic. Kevin Webb Swarthmore College January 26, 2016

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

Have examined process Creating program Have developed program Written in C Source code

CS 64 Week 1 Lecture 1. Kyle Dewey

More Programming Constructs -- Introduction

COSC121: Computer Systems: Runtime Stack

LECTURE 3 C++ Basics Part 2

Reserved Words and Identifiers

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

QUIZ: What value is stored in a after this

Chapter 7. Basic Types

Chapter 11 Introduction to Programming in C

UNIT- 3 Introduction to C++

Creating a C++ Program

ET156 Introduction to C Programming

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Computer Programming CS F111

Full file at

Chapter 7 Expressions and Assignment statements

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

2. Numbers In, Numbers Out

More about Binary 9/6/2016

Chapter 7. Expressions and Assignment Statements ISBN

Week 2 Introduction to Computer and Algorithm (Part 2)

Language Design COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

Software II: Principles of Programming Languages. Why Expressions?

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

3. Types of Algorithmic and Program Instructions

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Basics of Programming

Informatica e Sistemi in Tempo Reale

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Fundamentals of Programming

Transcription:

CS16 Week 2 Part 2 Kyle Dewey

Overview Type coercion and casting More on assignment Pre/post increment/decrement scanf Constants Math library Errors

Type Coercion / Casting

Last time... Data is internally represented as a series of bits The data type (i.e. char, int, etc.) determines how many bits are used

Recall unsigned char x = 255; x = x + 1; 11111111 + 1 ---------- 100000000 Size of Data Type

What about... Assume integers (int) are 4 bytes (32 bits) unsigned char x = 255; unsigned int y; y = x + 1; // what is the value of y?

Data Sizes It doesn t matter where it has been It only matters where it s going

Binary Operators When dealing with variables/expressions of different types, there are different rules for different operators Always go for the bigger type double > int > char The bigger type will be the type of the expression Going for a bigger type is called type coercion

Division int x = 5 / 2; int y = 6 / 2; int z = 7 / 0; double x = 5 / 2; double y = 6 / 2; double z = 7 / 0;

Division double x = 5 / 2; double y = 6 / 2; double z = 7 / 0; double x = 5 / 2.0; double y = 6 / 2.0; double z = 7 / 0.0;

Question double x = 5.5; x = x + 6 / 4; // what is the value of x?

Answer double x = 5.5; x = x + 6 / 4; x = x + (6 / 4); x = (x + ( 6 / 4 ); (x = (x + ( 6 / 4 ));

Answer (x = (x + ( 6 / 4 )); = x + x / 6 4

Answer (x = (x + ( 6 / 4 )); double = double double x + double int x / int int 6 4

Answer (x = (x + ( 6 / 4 )); 6.5 =?? 6.5 x + 5.5 1 x / 6 4 6 4

Question double x = 5.5; x = x + 6 / 4.0; // what is the value of x?

Answer (x = (x + ( 6 / 4.0 )); = x + x / 6 4

Answer (x = (x + ( 6 / 4.0 )); double = double double x + double double x / int double 6 4.0

Answer (x = (x + ( 6 / 4.0 )); 7.0 =?? 7.0 x + 5.5 1.5 x / 6 4 6 4

Casting Sometimes we want to specify the type explicitly Especially when we want to go down the chain (i.e. double to int) This can be done by putting the type itself in parenthesis before the operation

Casting Examples (double)5 // 5.0 (int)5.5 // 5? 6? (unsigned int)((unsigned char)255) (unsigned int)((unsigned char)256)

More on Expressions

Last time... Expressions return values Arithmetic consists of nested expressions 3 * 2 + 7-8 * 2

Assignment int x; x = 6 * 7;

Assignment Perfectly legal C: int x, y; x = y = 5 + 1; // what are the values // of x and y?

Assignment Expression x = y = 5 + 1; x = y = (5 + 1); x = (y = (5 + 1)); (x = (y = (5 + 1)));

Question Is this legal C? int x, y; x = y + 1 = 3 * 2;

Answer Is this legal C? int x, y; x = y + 1 = 3 * 2; No; the portion y + 1 = 3 * 2 has no meaning

Question int x, y; x = 5 + (y = 2 + 1) * 3; // what are the values // of x and y?

Question int x, y; x = 5 + y = 2 + 1 * 3; // what are the values // of x and y?

Answer int x, y; x = 5 + y = 2 + 1 * 3; // what are the values // of x and y? Trick question! This is an ambiguity that needs parenthesis

Precedences 1.() 2.*, /, % 3.+, - 4.=

Pre/post increment/ decrement

Pre/post inc/dec Specifically for variables Adding or subtracting one is so common there is are special shorthand operators for it Add one: ++ Subtract one: --

Pre-increment ++x Semantics: add one to x and return the resulting value

Pre-decrement --x Semantics: subtract one from x and return the resulting value

Post-increment x++ Semantics: return the current value of x and then add one to it

Post-decrement x-- Semantics: return the current value of x and then subtract one from it

Example #1 int x = 5; int y = x++ + 1; // what is x and y?

Example #2 int x = 5; int y = ++x + 1; // what is x and y?

Personally... Putting these directly in expressions can be very confusing Shorthand for x = x + 1, etc. Aside: people can introduce subtle bugs because of undefined behavior

Subtle Bug int x = 0; int y = x++ + x--; ch: -1 gcc: 0

Precedences 1.() 2.++,-- 3.*, /, % 4.+, - 5.=

scanf

scanf The dual to printf Instead of printing something to the terminal, it reads something from the terminal Understands placeholders Returns the number of items read in

Reading Something In Need a place to put it Adds a bit of complexity

Data vs. Data Location unsigned char foo = 0; foo; // what s the value of foo? &foo; // where is foo? foo???? 0????

Key Difference int input = 0; scanf( %i, input );... scanf( %i, &input );

Simple Examples int input1, input2; char character;... scanf( %i%i, &input1, &input2 ); scanf( %i%c, &input1, &character ); scanf( %i %i, &input1, &input2 );

On Whitespace scanf will ignore whitespace

Format String Can have non-placeholders in the format string Format string is a pattern of everything that must be read in (whitespace treated equally) int input1, input2; scanf( %ifoo%i, &input1, &input2 );

scanf.c

Constants

Constants Values which never change Specific values are constants 55 27.2 a foobar

Constants Specifically in the program text Constant in that 52 always holds the same value We cannot redefine 52 to be 53

Symbolic Constants Usually when programmers say constant, they mean symbolic constant Values that never change, but referred to using some symbol i.e. π (pi - 3.14...) Mapping between symbol and value is explicitly defined somewhere

In C Use #define By convention, constants should be entirely in caps #define PI 3.14... int x = PI * 5;

Mutability Constants are, well, constant! Cannot be changed while code runs #define PI 3.14... PI = 4; // not valid C!

What #define Does Defines a text substitution for the provided symbol This text is replaced during compilation by the C preprocessor (cpp)

Example #1 #define PI 3.14 Code... int x = PI * 5; After Preprocessor int x = 3.14 * 5;

Example #2 #define PI 3.14 Code... PI = 4; After Preprocessor 3.14 = 4;

Best Practices Generally, all constants should be made symbolic Easier to change if needed later on Gives more semantic meaning (i.e. PI is more informative than 3.14...) Possibly less typing

Errors

Errors Generally, expected result does not match actual result Four kinds of errors are relevant to CS16: Syntax errors Linker errors Runtime errors Logic errors

Errors Four kinds of errors are relevant to CS16: Syntax errors Linker errors Runtime errors Logic errors

Syntax Error A sentence was formed that does not exist in the language For example, Be an awesome program isn t valid C

Syntax Error Easiest to correct Compiler will not allow it *Usually* it will say where it is exactly

On Syntax Errors...sometimes the compiler is really bad at figuring out where the error is #include <stdio.h> int main() { printf( "moo" ) printf( "cow" ); return 0; }

Reality #include <stdio.h> int main() { printf( "moo" ) printf( "cow" ); return 0; } Missing semicolon at line 4

GCC #include <stdio.h> int main() { printf( "moo" ) printf( "cow" ); return 0; } syntax.c: In function main : syntax.c:5: error: expected ; before printf

Ch #include <stdio.h> int main() { printf( "moo" ) printf( "cow" ); return 0; } ERROR: multiple operands together ERROR: syntax error before or at line 5 in file syntax.c ==>: printf( "cow" ); BUG: printf( "cow" )<==???

The Point Compilers are just other programs Programs can be wrong Programs are not as smart as people

Errors Four kinds of errors are relevant to CS16: Syntax errors Linker errors Runtime errors Logic errors

Recall Linking 1: somethingfromhere(); 2: somethingfromelsewhere(); 3: somethingelsefromhere(); somethingfromhere somethingfromelsewhere somethingelsefromhere

Recall Linking somethingfromelsewhere somethingfromhere somethingelsefromhere

Linker Errors What if somethingfromelsewhere is nowhere to be found? Missing a piece Cannot make the executable

Example int something(); int main() { something(); return 0; } int something(); tells the compiler that something exists somewhere, but it does not actually give something

Example int something(); int main() { something(); return 0; } Undefined symbols for architecture x86_64: "_something", referenced from: _main in ccm6c8aw.o ld: symbol(s) not found for architecture x86_64

Errors Four kinds of errors are relevant to CS16: Syntax errors Linker errors Runtime errors Logic errors

Runtime Errors Error that occurs while the code is running Compilation and linking must have succeeded to get to this point

Examples Overflow unsigned char x = 255; x = x + 1; Underflow unsigned char x =0; x = x - 1;

Examples Divide by zero (especially for integers!) unsigned int x = 5 / 0; Wrong printf placeholder printf( %s, 57 );

Errors Four kinds of errors are relevant to CS16: Syntax errors Linker errors Runtime errors Logic errors

Logic Errors It works!...but it doesn t do what you wanted Like getting the wrong order at a restaurant

Examples Transcribed an equation incorrectly Using the wrong variable Lack of understanding of problem etc. etc. etc...

Logic Errors By far, the most difficult to debug It might be done almost correctly This is why testing is so important!