CS16 Week 2 Part 2 Kyle Dewey. Overview Type coercion and casting More on assignment Pre/post increment/decrement scanf Constants Math library Errors.

Slides:



Advertisements
Similar presentations
Week 1 Part II Kyle Dewey. Overview Lab The art of programming Basic data types Variable declaration and initialization Data representation.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
C Language Elements (II) H&K Chapter 2 Instructor – Gokcen Cilingir Cpt S 121 (June 22, 2011) Washington State University.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
The scanf Function The scanf function reads input from the standard input device into one or more variables Example: scanf(“%lf”, &miles); Reads a real.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
String Escape Sequences
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Basic Elements of C++ Chapter 2.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
Week 3 Part I Kyle Dewey. Overview Odds & Ends Constants Errors Functions Expressions versus statements.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
Week 1 Algorithmization and Programming Languages.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
Errors “Computer says no..”. Types of Errors Many different types of errors new ones are being invented every day by industrious programming students..
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Introduction to Programming
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 3: Introduction to C: Input & Output, Assignments, Math functions.
Khalid Rasheed Shaikh Computer Programming Theory 1.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Week 7 Part I Kyle Dewey. Overview Code from last time Array initialization Pointers vs. arrays Structs typedef Bubble sort (if time)
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Gator Engineering Project 1 Grades released Re-grading –Within one week –TA: Fardad, or office hours: MW 2:00 – 4:00 PM TA Huiyuan’s office hour.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Last week: We talked about: History of C Compiler for C programming
Week 3 Part 2 Kyle Dewey.
CSC201: Computer Programming
Chapter 2 - Introduction to C Programming
ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Expressions and Assignment
elementary programming
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

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; Size of Data Type

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

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 x = x + 6 / 4; x = x + (6 / 4); x = (x + ( 6 / 4 ); (x = (x + ( 6 / 4 )); double x = 5.5;

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

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

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

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

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

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

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

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

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 = * 3; // what are the values // of x and y?

Answer int x, y; x = 5 + y = * 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

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? 0???????? foo

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

Values which never change Specific values are constants ‘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 ) Mapping between symbol and value is explicitly defined somewhere

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

Mutability Constants are, well, constant! Cannot be changed while code runs #define PI 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 int x = PI * 5; Code int x = 3.14 * 5; After Preprocessor

Example #2 Code 3.14 = 4; After Preprocessor #define PI PI = 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 ) Possibly less typing

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 int main() { printf( "moo" ) printf( "cow" ); return 0;}

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

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

Ch #include 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(); somethingFromHer e somethingElseFromHer e somethingFromElsewh ere

Recall Linking somethingFromHer e somethingElseFromHer e somethingFromElsewhere

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.old: 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!