Lecture 3: More on operators and expressions ECE 209 January 20, 2010 ECE 209: Computer Systems Programming Tuck Assignments ?HW0 -- Due 3/28 ?HW1 -- Due 1/25 ?CodeLab ? Ch 2,3 -- Jan 25 ?Reading: ? PP: Ch 12, C: 2,3,5 ? For next time: PP: 13, C: 6 2 ECE 209: Computer Systems Programming Tuck Types of data ?More about types ? char = signed character, at least 8 bits (usually assumed to be 1 byte) ? ASCII character: ?A? -- place one character in single quotes (**preferred coding) ? Decimal value: 65 ? Octal ASCII value: ?\0101? ? Hexadecimal ASCII value: ?\x41? ? Also other special characters: ?\n? -- newline, ?\t? -- tab 3 ECE 209: Computer Systems Programming Tuck Types of data ?More about types ? double (or float) = signed floating point ? Decimal: 1.23 ? Scientific: 1.0e23 , which means 1.0 x 10 23 ? Other Examples ? 2.0E20 ? 3.4e-9 ? -1. ? .23 ? 3.14159e0 4 ECE 209: Computer Systems Programming Tuck Excercise ?Declare a variable to hold Avogadro?s number. 5 ECE 209: Computer Systems Programming Tuck Printing other types #include int main(void) { int days_in_a_week = 7; double avocado = 6.02e23; char initial = ?T?; /* need a different escape code for each type so that printf knows how to treat it */ printf(?days in a week = %d\n?, days_in_a_week); printf(?Avocado?s Number = %e\n?, avocado); printf(?My last initial = %c\n?, initial); return 0; /* exit the program */ } 6 ECE 209: Computer Systems Programming Tuck Operators ?C provides operators to manipulate variables of all types (unless specified otherwise) ? Assignment ? Arithmetic operations: addition, subtraction, multplication, division, etc. ? Logical operations: and, or, negation ? Bitwise operators: and, or, xor, inversion ? Parenthesis to group operations ? Relational operators: less than, greater than, equal, not equal 7 ECE 209: Computer Systems Programming Tuck Operators --- actions to be performed on data ?Assignment: lvalue = rvalue ? Rvalue: the right side of an assigment, it can be any value ? Lvalue: must be a variable (is associated with a memory location) ? After the operation, Lvalue is equivalent to rvalue x = 100; y = 5; x = y * 2; /* after this statement, x contains 10 */ 8 ECE 209: Computer Systems Programming Tuck Arithmetic operators ?Binary arithmetic operations: +, -, *, /, % ? Addition: + ? Subtraction: - ? Division: / ? For integers, it throws away the remainder: 7/3 = 2 ? For floats, 7/3 = 2.333... ? Modulo: % ? Computes the remainder and is only available for int and char ? If mixed types are in a C operation, smaller precision is promoted to larger precision (e.g. int is converted to float, char is promoted to int) 9 ECE 209: Computer Systems Programming Tuck Arithmetic operators ?Unary operators ? + : positive ? - : negative 10 ECE 209: Computer Systems Programming Tuck C Expressions ?An expression can be one of the following things in C ? a variable ? a value (literal) ? an operation on values or variables or both ? a function call ? an operation on expressions (yes, this is a recursive definition) ? Important: An expression is NOT a statement ? But, you can add a semicolon to any expression to create a statement 11 ECE 209: Computer Systems Programming Tuck More on expressions ?Which of the following are expressions, and if so, what are their types? printf(?I?m an expression?) 0+1 ?A?; ?A? * 3 x = 3.5e4-.5; 12 ECE 209: Computer Systems Programming Tuck How to interpret an expression ?What does this expression evaluate to: 2 + 3 - 4 + 5 13 ECE 209: Computer Systems Programming Tuck Operator precedence ?Precedence determines the order operators are evaluated ? I.e. it answers the question: where are the implicit parentheses? ? Provides a rigorous way for programmer & compiler to agree on the meaning ?Precedence becomes more important as we learn more operators ? For arithmetic operations precedence works as you expect (from algebra) 14 ECE 209: Computer Systems Programming Tuck Precedence table 15 Operators Associativity () Left to Right + - (Unary) Right to Left * / Left to Right + - (Binary) Left to Right = Right to Left Highest Precedence (Apply first) Lowest Precedence (Apply last) ECE 209: Computer Systems Programming Tuck Example a + b - c + d 16 ECE 209: Computer Systems Programming Tuck Example y = 6 * 12 + 5 * 20 + 1 17 ECE 209: Computer Systems Programming Tuck Example x = y = x + y 18 James Tuck ECE209_Lecture3.key
STUDYBLUE makes things that make you better at school.
Things like
online flashcards with photos and audio.
Things like personalized quizzes and friendly reminders about when (and what) to study next.
Think of it as a digital backpack™: access to all of your study materials online and on your phone.
STUDYBLUE exists to make studying efficient and effective for every student, for free.
Join us.
“Simply amazing. The flash cards are smooth, there are many different types of studying tools, and there is a great search engine. I praise you on the awesomeness.”
Dennis