C expressions (Reek, Ch. 5) 1CS 3090: Safety Critical Programming in C.

Slides:



Advertisements
Similar presentations
ISBN Chapter 7 Expressions and Assignment Statements.
Advertisements

IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
ISBN Chapter 7 Expressions and Assignment Statements.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
ISBN Chapter 7 Expressions and Assignment Statements.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Copyright © 1998 by Addison -Wesley Longman, Inc. 1 Chapter 6 Arithmetic Expressions - Their evaluation was one of the motivations for the development.
7-1 Chapter 7: Expressions and Assignment Statements Arithmetic Expressions –the fundamental means of specifying computations in a programming language.
Chapter 7 Expressions and Assignment Statements. Chapter 7 Topics 1-2 Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
OperatorstMyn1 Operators The sequence in which different operators in an expression are executed is determined by the precedence of the operators. Operators.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Bitwise Operators in C. Bitwise operators are used to manipulate one or more bits from integral operands like char, int, short, long.
Arithmetic Expressions
Expressions and Assignment Statements
Chapter 7 © 1998 by Addison -Wesley Longman, Inc Arithmetic Expressions - Their evaluation was one of the motivations for the development of the.
ISBN Chapter 7 Expressions and Assignment Statements.
Expressions and Operators in C. Expressions and Operators Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); Two types: – Function calls – The expressions.
Operators & Expressions
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Expression and Operator. Expressions and Operators u Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); u Two types: –Function calls –The expressions formed.
ISBN Chapter 7 Expressions and Assignment Statements.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
CHAPTER 4 CS 3370 – C++ Expressions. Operators Unary -, *, ++, -- higher precedence than binary operators most associate right-to-left Binary most associate.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
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.
ME2008– W05 MID1- Reference 2016Q1- Source: Deitel /C- How To.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Expressions and Assignment Statements
Expressions and Assignment Statements
7.2 Arithmetic Expressions
CSE 220 – C Programming Expressions.
Chapter 7: Expressions and Assignment Statements
Expressions and Assignment Statements
Chap. 2. Types, Operators, and Expressions
University of Central Florida COP 3330 Object Oriented Programming
CSCI206 - Computer Organization & Programming
University of Central Florida COP 3330 Object Oriented Programming
Chapter 7: Expressions and Assignment Statements
Expressions and Assignment Statements
What to bring: iCard, pens/pencils (They provide the scratch paper)
Expressions and Assignment Statements
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
All the Operators 22-Nov-18.
More about Numerical Computation
Expressions and Assignment Statements
All the Operators 4-Dec-18.
Expressions and Assignment Statements
Associativity and Prescedence
Homework Homework Continue Reading K&R Chapter 2 Questions?
Chapter 7 Expressions and Assignment Statements.
All the Operators 6-Apr-19.
All the Operators 13-Apr-19.
Chapter 4: Expression and Operator
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Expressions and Assignment Statements
Bit Manipulations CS212.
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

C expressions (Reek, Ch. 5) 1CS 3090: Safety Critical Programming in C

Shift operations CS 3090: Safety Critical Programming in C2  Left shift: value << n  discard the n leftmost bits, and add n zeroes to the right  Right shift: value >> n  Two definitions:  logical version: discard the n rightmost bits, and add n zeroes to the left  for negative values, the sign bit is the leftmost bit – so logical right shift has the effect of making the value positive  arithmetic right shift: like logical right shift, but maintain sign bit  The distinction is only relevant for negative values

Right shift: example CS 3090: Safety Critical Programming in C3 INT_MAX INT_MAX >> INT_MIN logical right shift: INT_MIN >> arithmetic right shift: INT_MIN >>

Bitwise operations CS 3090: Safety Critical Programming in C4  Binary operators &, |, ^  perform bitwise and, or, xor on each bit of the operands  Unary operator ~  perform one’s complement of the operand: change each 0 to a 1 and vice versa & | ^

Bitwise operators: Example CS 3090: Safety Critical Programming in C5 a: (0x2E)b: (0x5B) ~a: (0xD1)~b: (0xA4) a&b a|b a^b (0x0A)(0x7F)(0x75)

Example: setting a bit to one CS 3090: Safety Critical Programming in C6 value = value | 1 << bit_number; 0xAA|1 <<

value = value & ~ ( 1 << bit_number ); 0xAA&~(1 << 3) Example: setting a bit to zero CS 3090: Safety Critical Programming in C7

Assignment is an expression CS 3090: Safety Critical Programming in C8  In many languages, expressions only compute values  They never have side effects: lasting changes to the program state (e.g. update a variable, print a symbol).  C (like Java and C++) does have side effects in expressions – notably, assignment expressions (e.g. ==, +=, ++ )

Why does x = y = z = 17 work? CS 3090: Safety Critical Programming in C9  = is right-associative – so the expression is interpreted as:x =( y =( z = 17 ) z is assigned 17; return value is 17 y is assigned 17; return value is 17 x is assigned 17; return value is 17

Another example of = as subexpression CS 3090: Safety Critical Programming in C10  The stdio library function getchar() returns a character value (read from the input), or the value EOF if end-of-file is detected. intch; while ( (ch = getchar()) != EOF) {... Use the character stored in ch... }  Note: ch is an int because EOF is larger than any char value

Precedence, associativity, evaluation order CS 3090: Safety Critical Programming in C11  Precedence: given two operators of different types, which is evaluated first?  Associativity: given a sequence of instances of the same operator, are they evaluated left-to-right or right-to-left?  Evaluation order: given an operator with a number of operands, in what order are the operands evaluated?

Example: Expression evaluation CS 3090: Safety Critical Programming in C12 a * b + c * d + e * f  * has precedence over +  So, multiplications are performed before additions  This leaves us with a sequence of two additions  + has left-to-right associativity, so do the leftmost addition first  Note: the multiplications can be done in any order  * has left-to-right associativity, but it doesn’t matter, since we don’t have a sequence of multiplications

Side effects and evaluation order CS 3090: Safety Critical Programming in C13 int i = 10; i = i i * ( i = -3 ) * i i;  What are the possibilities?  Note: ++i increments i and returns the incremented value;  i++ does the same but returns the value before the increment

&& and || have “short circuit” evaluation CS 3090: Safety Critical Programming in C14  left operand evaluated first; right operand evaluated only if necessary  && : if left operand evaluates to 0, immediately return 0  || : if left operand evaluates to nonzero value, immediately return 1  This allows you to write code like this with confidence: if(y == 0 || x / y > MAX)...

Conditional operator: order of evaluation CS 3090: Safety Critical Programming in C15  Ternary operator: a ? b : c  If a evaluates to nonzero, evaluate b and return its value  Else evaluate c and return its value  Only two of the three operands are evaluated at one time if (a)vs.result = a ? b : c; result = b; else result = c;  But avoid abuse of conditional operator  e.g. nested conditionals – yikes! More concise, less room for typos – particularly if result is a complex expression

Comma operator CS 3090: Safety Critical Programming in C16  a, b :evaluate a, then evaluate b and return b ’s value  Useful only if a has a side effect