Unformatted text preview:

University of Washington Computer Programming I Overview Concepts this lecture Variables Declarations Identifiers and Reserved Words Types Expressions Assignment statement Variable initialization Lecture 3 Variables Values and Types 2000 UW CSE C 1 Review Computer Organization C 2 Review Memory Memory is a collection of locations Monitor Central Processing Unit Disk Within a program the locations are called variables Each variable has Main Memory Keyboard mouse A name an identifier A type the kind of information it can contain Basic types include Network C 3 int integers whole numbers 17 42 double floating point numbers with optional fraction and or exponent 3 14159 6 02e23 char character data a N 9 C 4 Memory example Declaring Variables Variable declarations in C int months Integer variables represent whole numbers int i 12 double gasPrice 1 799 char bang Picture i gasPrice bang 1 17 32 0 double pi Not 1 5 2 0 A Floating point variables represent real numbers 12 int 3 14 27 5 6 02e23 5 0 Not 3 char first initial middle initial marital status 1 799 double Character variables represent individual keyboard char C 5 characters a b M 0 9 Not Bill C 6 C 1 Variable Names Reserved words Identifiers are names for things in a program for examples names of variables In C identifiers follow certain rules Certain identifiers have a reserved permanent special meaning in C We ve seen int already Will see a couple of dozen more eventually These words always have that special meaning and cannot be used for other purposes Cannot be used names of variables Must be spelled exactly right Sometimes also called keywords use letters numerals and underscore do not begin with a numeral cannot be reserved words are case sensitive can be arbitrarily long but Style point Good choices for identifiers can be extremely helpful in understanding programs Often useful noun or noun phrase describing variable contents C 7 C 8 Under the Hood Assignment Statements All information in the CPU or memory is actually a series of bits 1 s and 0 s An assignment statement stores a value into a variable The assignment may specify a simple value to be stored or an expression Known as binary data Amazingly all kinds of data can be represented in binary numbers letters sounds pictures etc The type of a variable specifies how the bits are interpreted Binary C type sample value 01010001 int 161 char A int area length width length 16 width 32 area length width declaration of 3 variables length gets 16 width gets 32 area gets length times width Execution of an assignment statement is done in two distinct steps double 10 73 Normally we ignore the underlying bits and work with C types C 9 Evaluate the expression on the right hand side Store the value of the expression into the variable named on the left hand side C 10 my age my age 1 Program Execution This is a statement not an equation Is there a difference The same variable may appear on both sides of an assignment statement A memory location is reserved by declaring a C variable You should give the variable a name that helps someone else reading the program understand what it is used for in that program my age my age 1 balance balance deposit The old value of the variable is used to compute the value of the expression before the variable is changed You wouldn t do this in algebra C 11 Once all variables have been assigned memory locations program execution begins The CPU executes instructions one at a time in order of their appearance in the program we will introduce more options later C 12 C 2 Hand Simulation Trace An Example calculate and print area of 10x3 rectangle include stdio h int main void int rectangleLength int rectangleWidth int rectangleArea rectangleLength 10 rectangleWidth 3 rectangleArea rectangleLength rectangleWidth printf d rectangleArea return 0 A useful practice is to simulate by hand the operation of the program step by step This program has three variables which we can depict by drawing boxes or making a table We mentally execute each of the instructions in sequence and refer to the variables to determine the effect of the instruction C 13 Tracing the Program C 14 Tracing the Program rectangleLength rectangleWidth rectangleArea after declaration after statement 1 10 rectangleLength rectangleWidth rectangleArea after declaration after statement 1 10 after statement 2 10 3 after statement 3 10 3 30 C 15 C 16 Initializing Variables Initialization Rule Initialization means giving something a value for the first time General rule variables have to be initialized before their value is used Anything which changes the value of a variable is a potential way of initializing it For now that means assignment statement Failure to initialize is a common source of bugs is a semantic error not a syntax error Variables in a C program are not automatically initialized to 0 C 17 C 18 C 3 Example Problem Fahrenheit to Celsius Declaring vs Initializing int main void double income declaration of income not an assignment or initialization income 35500 00 assignment to income initialization of income not a declaration printf Old income is f income income 39000 00 assignment to income not a declaration or initialization printf After raise f income Problem specified Convert Fahrenheit temperature to Celsius return 0 C 19 C 20 Fahrenheit to Celsius I An actual C program Example Problem Fahrenheit to Celsius include stdio h int main void Problem specified Convert Fahrenheit temperature to Celsius double fahrenheit celsius Algorithm result of analysis Celsius 5 9 Fahrenheit 32 celsius fahrenheit 32 0 5 0 9 0 What kind of data result of analysis double fahrenheit celsius return 0 C 21 Fahrenheit to Celsius II C 22 Running the Program include stdio h int main void double fahrenheit celsius printf Enter a Fahrenheit temperature scanf lf fahrenheit celsius fahrenheit 32 0 5 0 9 0 printf That equals f degrees Celsius celsius return 0 Enter a Fahrenheit temperature 45 5 That equals 7 500000 degrees Celsius Program trace after declaration after first printf after scanf after assignment after second printf C 23 fahrenheit celsius 45 5 45 5 45 5 7 5 7 5 C 24 C 4 Assignment step by step Fahrenheit to Celsius III include stdio h celsius fahrenheit 32 0 5 0 9 0 1 Evaluate right hand side a Find current value of fahrenheit b Subtract 32 0 b Multiply by 5 0 c Divide by 9 0 72 0 40 0 200 0 22 2 2 Assign 22 2 to be the new value of celsius the old value of celsius is lost C 25 Does Terminology Matter


View Full Document

UW CSE 142 - Study Notes

Loading Unlocking...
Login

Join to view Study Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Notes and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?