lect04

# lect04 - CMSC 216 Introduction to Computer Systems Lecture...

This preview shows pages 1–6. Sign up to view the full content.

9/15/11 1 CMSC 216 Introduction to Computer Systems Lecture 4 Introduction to C Jan Plane & Pete Keleher {jplane, keleher}@cs.umd.edu Administrivia • Project 1 questions o read what is on piazza o see TAs or instructors in office hours • Read Chapter 5 of Reek – parts of Chapters 8 and 9 are also covered today and Thursday CMSC 216 - Wood, Sussman, 2

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
9/15/11 2 Last Time: Other unary operators ( typename ) is a unary operator – Works just as in Java • There is also a - operator which performs arithmetic negation – so code like " a *= -1; " is really just wasteful • We'll discuss the unary and * operators soon, when we discuss pointers 3 The sizeof operator • Unary operator, evaluates to the number of bytes necessary to hold its operand • Operand can be an expression or a type name • Does NOT evaluate the expression • Examples: int i = 5; printf("%d\n", sizeof(i)); printf("%d\n", sizeof(unsigned char)); printf("%d\n", sizeof(i++)); printf("%d\n", i); 4
9/15/11 3 Boolean operators • Relational operators: < , > , <= , >= • Equality operators: == , != • Logical operators: , || , ! • Function just as you'd expect from working in Java, except that they evaluate to 1 (if true) or 0 (if false), so this example actually makes sense: int i; i = (! 3) == (4 < 2); i = (! 2) || (5 && i); • Remember that the logical operators do short- circuit, affecting whether or not parts of expressions get evaluated 5 Conditional operator • The only ternary operator • Syntax: expr1 ? expr2 : expr3 • If expr1 is nonzero, evaluates to expr2; otherwise, evaluates to expr3 • Don't abuse this; use it only when it helps reduce code duplication: if (a > 5) b[2 * c + f(d / 5)] = 3; else b[2 * c + f(d / 5)] = -20; 6

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
9/15/11 4 Comma operator • Yes, the comma is an operator • Evaluates left operand, then right operand • Value of expression with comma is value of last operand • Has lowest precedence of all operators • So what gets stored in i after each statement? What does each statement evaluate to? i = 1, 2, 3, 4; i = (1, 2, 3, 4); 7 Precedence and associativity • Different operators can fall on different precedence levels • Ties among levels are settled by associativity rule for that level • Some operators impose restrictions on evaluation order, but aside from that, compiler can optimize • Full table in Pointers on C , pgs. 114-115 Use parens! What about ‘b = a * 3 + 4 * a++’ ?
9/15/11 5 Lvalues and Rvalues • An rvalue is anything that can appear on the right side of an assignment statement – virtually any expression • An lvalue is anything that can appear on the left side of an assignment statement – values that represent a place to store a value • The right and left sides of an assignment statement are treated differently – right hand side is a value, left hand side is a location to

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 17

lect04 - CMSC 216 Introduction to Computer Systems Lecture...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online