300 likes | 400 Views
EEE 243B Applied Computer Programming. Expressions and Operators §3.1 – 3.4 Prof Sylvain P. Leblanc. Review. What are the four types found at the first level of the type hierarchy in C? What are the three Integral types and the Floating-point type we are concerned with?
E N D
EEE 243BApplied Computer Programming Expressions and Operators §3.1 – 3.4 Prof Sylvain P. Leblanc
Review • What are the four types found at the first level of the type hierarchy in C? • What are the three Integraltypes and the Floating-point type we are concerned with? • What are the values of true and false for the bool type in C? • Why is it not a good idea to use literals (such as 0.07) in your code instead of a defined constant or a memory constant? Prof S.P. Leblanc
Outline • Expressions in C • postfix • prefix • unary • binary • Assignments • Compound Assignments • Types Specific Operations • Operators, Precedence and Associativity • Side Effects Prof S.P. Leblanc
A Caveat… • Because you have a knowledge of programming, in this course we concentrate on what makes C different than Matlab and other programming languages. • Some of what you will see in the next few weeks will be a review of what you already know with some syntactical differences. Prof S.P. Leblanc
1 - Expressions in C • There are two main things that separate C from many other programming languages: • Expressions • Pointers • Today we will look at the power that C provides to write expressions Prof S.P. Leblanc
Expressions in C • An expression is a sequence of operands and operators that evaluates to a single value. • All the following are valid expressions • (a + b) • 4 + 5 • a++ • --c • x = a + b + c • y *= z • !(a > b || c) • -a Prof S.P. Leblanc
1 - Expressions in C Primary Identifier, constant, or parenthetical expression a, 5, (5+a–z) a++ ++b, !f, -a sizeof (int) 5 + 3, t || f x = y + 3 Ternary Will be discussed during next class Prof S.P. Leblanc
The postfix expression • Operators that are placed after a variable form postfix expressions: • a++ , a-- • Example: if x is to have the value of a and then have a incremented after the assignment • In many languages: (initial value of a = 10) • x = a; //.. Value of a and x 10 • a = a + 1; //.. a = 11 • In C: • x = a++; // same result as above Prof S.P. Leblanc
The prefix expressions • Operators that are placed before a variable form prefix expressions: • ++a, !a, -a • Example: if a is incremented and then assigned to x • In many languages: (initial value of a = 10) • a = a + 1; //.. a = 11 • x = a; //..Value of a and x 11 • In C: • x = ++a; // same result as above Prof S.P. Leblanc
The unary expression • In unary expressions, operators can be placed before an expression (this is different from the prefix expression where the operator is placed before a variable) • sizeof() //also a unary expression • sizeof(int) • sizeof(x) • x = sizeof(3.1416) • sizeof((a + b -1)+(2-18.3)) • sizeof() looks like a function call but it is treated as an operator in that it has a different priority than a function call Prof S.P. Leblanc
The binary expression • Operators that are placed between variables form binary expressions: • x + 1, y * 3 • A variable assignment is a form of binary expression: • x = 1, y = 3 Prof S.P. Leblanc
Compound assignments • In MATLAB: the assignment operator is = • x = x + 1; y = y * 3; • ‘C’ has compound assignments for these operations Prof S.P. Leblanc
Types Revisited • Remember that a type defines • a set of values and • a set of operations • Intrinsic types in C • void • char • bool • Integers • short int, int, long int, long longint • Floating point • float, double, long double • We will examine some of the type specific operations in C Prof S.P. Leblanc
2 - Type Specific Operations • ‘C’ includes a set of arithmetic, Boolean and logical operators • As just mentioned, the functionality of the operators depends upon type of data Arithmetic: + - * / % Boolean: & | ! && || ^ Logical: == != > < >= <= Prof S.P. Leblanc
Valid void operators • void has no values and NO operators • void is very useful for pointers which we will cover later Operators: Prof S.P. Leblanc
Valid bool Operators • The Boolean type (bool) is a recent addition to C • int is actually used for logical data • The use of the type bool requires the inclusion of stdbool.h • Defines bool, true and false • false is defined as int 0 • true is defined as int 1 (1 == 1) true (1 != 1) false (i = 0) false Prof S.P. Leblanc
Valid Integer and char operator • Integers and char have the same set of valid operators Arithmetic: Addition + Subtraction – Multiplication * Division / Modulus % Pre/post incr ++ Pre/post decr -- Boolean: AND && OR || NOT ! Bitwise AND & Bitwise OR | Bitwise XOR ^ Prof S.P. Leblanc
Valid Integer and char operator • Integers and char have the same set of valid operators Logical: Greater than > Greater than or equal >= Less Than < Less Than or equal <= Equal to == Not Equal != WARNING: = and == are NOT THE SAME Prof S.P. Leblanc
Notes on Integer Operators • % Modulus • Returns the remainder of an INTEGER division • Ex: knowing that 7/4 = 1 remainder 3 • 7%4 returns a value of 3 • 7/4 returns a value of 1 • Power: THERE is NO POWER OPERATOR IN ‘C’ • ^ is a bitwise XOR • In MATLAB for x2 we can code it as • x^2 • In C for x2 we can code it as • x*x NOTE: There is a pow() function in <math.h> Prof S.P. Leblanc
Valid floating point operators • Floating point values have the following valid operators Arithmetic: Addition + Subtraction – Multiplication * Division / Modulus % Pre/post incr ++ Pre/post decr -- Boolean: AND && OR || NOT ! Bitwise AND & Bitwise OR | Bitwise XOR ^ Prof S.P. Leblanc
Floating point logical operators • Floating point values have the following valid operators Logical: >, >=, <, <=, !=, == Warning: == It is not recommended to use == for float types 1.1e-19 does not exactly equal 0 It is recommended to use a range fabs(x)<1e-7 Prof S.P. Leblanc
3 - Precedence of operators • From your CSE101 introductory programming course, you will know many of the operators that you will need during this course • Except for the bit, address and pointer operators • If there are several operators in an expression the precedence determines the order in which the operators are evaluated • If you cannot remember, there is a table on the inside cover of Forouzan. Prof S.P. Leblanc
4 - Associativity • Associativity is used to determine the order in which operators of the same precedence are evaluated in complex expressions • Most expressions use left-to-right associativity: • 3 * 8 / 4 % 4 * 5 is equivalent to • ((((3*8)/4)%4)*5) Prof S.P. Leblanc
Right-to-Left Associativity • Right-to-left associativity is used only for three types of expressions: • Unary (simple case), • Ternary (next class), and • Assignment expression associativity • a+=b*=c-=5 Is the same as: • (a=a+(b=b*(c=c–5))) Prof S.P. Leblanc
Operators, Precedence & Associativity • Always remember: first precedence than associativity • If you look at the precedence table, parenthetical expressions are at the top of the food chain. • When in doubt about the order of evaluation of any expression, use parentheses • The good thing about using the parentheses is … the compiler checks them! Prof S.P. Leblanc
4 - Side Effects • Some of the operators modify the value in memory of the variable they operate on • This modification of memory content is called a side effect • For example, x = ++a; has 2 side effects • a is pre-incremented • x is ‘assigned’ the new value of ‘a’ Prof S.P. Leblanc
Practice Problems • Questions involving precedence and evaluating expressions are VERY OFTEN on the MIDTERM TEST and FINAL EXAM • Should you choose to complete the following, I will mark them: • 3.17 • 3.22 • 3.23 (assume int x, y, z) • 3.23 (assume float x, y, z) • 3.24 Prof S.P. Leblanc
Practice Problems (Continued) • If originally a=2, b=5, c=1and d= -1, what is the value of each of the following expressions? (if inta,b,c,d; ) • a=b+=c+d • c=d+=a*=b%=3 • a+=(a==b)*c*=-2+d; • a>b + b>c + c>d + d>a • For char a =’d’; int b=6; float c = 3.161524; what are the values of the following expressions? • char d; • int e; • float f; • d = a*b; • e = a*b; • d= a*c; • e=a*c • f = a*c; • f =c%2; Prof S.P. Leblanc
Quiz Time • What is the value of this expressions • 4 + 2 * 5 • If a = 3 and b = 5 what are their value after: • x = --a + b++ • b += a-- • y = -b++ Prof S.P. Leblanc
Next Lecture • Mixed Expressions and Blocks Prof S.P. Leblanc