department of electronic & electrical engineering expressions operators operands precedence...
DESCRIPTION
Department of Electronic & Electrical Engineering Example: 2.0*x*sin(z*y) constant variable another expression! function 2.0 *x * ( z*y sin ) order of evaluationTRANSCRIPT
Department of Electronic & Electrical Engineering
Expressions operators operands precedence associativity types.
Department of Electronic & Electrical Engineering
Expressions
• An expression is comprises:• variables• constants• function calls (must return a value)• operators (unary or binary)
operands
Department of Electronic & Electrical Engineering
Example:
2.0*x*sin(z*y)
constant
variable another expression!
function
2.0 *x * ( z*y sin )
order of evaluation
Department of Electronic & Electrical Engineering
Arithmetic Operators
Arithmetic operators + * - / can be used with any of the fundamental types (int char float double).
% can only be used with integer types.
Department of Electronic & Electrical Engineering
Pre/Post fix operators -- ++
Increment / decrement a either before or after the value of the operand
a = b++ ;
assigns the a with the value of b then increments b.
a = ++b ;
increments the value of b then assigns the new value to a
i++ ; /* increment i */
Department of Electronic & Electrical Engineering
Precedence
• Expressions are usually evaluated from left to right (associativity)• If an expression has different operators then precedence rules are
applied• Brackets can be used to form sub expressions (override precedence)• For example arithmetic operators the * / and % operators will be
evaluated before + or –
e.g. 4+6/2 is equivalent to 4+ (6/2) 4-6+2 (4-6)+2 4/6 - 2 (4/6)-2
Department of Electronic & Electrical Engineering
Combining different types.
• Both operands the same type results is the same. (2/3 0)• integer and floating point type t result will be floating point. e.g.
3.0/2 -> 1.5• When different sizes of the same type are used the result will be the
type of the longest operand.• These rules are applied to each step as an expression is evaluated e.g. 3/4*4.0 will give the result zero. ( because 3/4 0 )• For the above you could change the order of evaluation e.g. 4.0*3/4• When in doubt use brackets ! e.g. (4.0*3)/4
• 3/4*4.0 != 3/(4*4.0)
Department of Electronic & Electrical Engineering
Examples
4*5.0+6
7/8*34
x*y*func(4,5)
5%2
(78+84)*(34-45)
int func(int a,int b) { return a*b-7;}
Department of Electronic & Electrical Engineering
C and boolean variables (TRUE and FALSE)
• unlike some other languages C does not have a Boolean type.
• C uses int• any non zero value is interpreted as TRUE.• zero is interpreted as FALSE.
Department of Electronic & Electrical Engineering
Relational (comparator) operators
e.g. 5.0 > 4.0 1 for TRUE 6.0==7.0 0 for FALSE
int result 1 or 0
Department of Electronic & Electrical Engineering
Logical Operators (combining boolean values)
1 && 0 0
!( 1 || 0) 0
7 && 5 1
Department of Electronic & Electrical Engineering
Logical examples.
int x=5,y=6,z=7; /* we can declare and initialize at the same time ! */int result; /* C does not have a boolean type. We use int */
result = x>y; /* result should be 0 which means FALSE */result = x<y; /* result should be 1 which means TRUE */
result = (x > y) && ( ! z < x) ; /* Hmmm this is a bit tricky */
result = (x > y) && ( ! (z < x)) ; /* I think this is what I meant to write */
Department of Electronic & Electrical Engineering
Operator PrecedenceHigher precedence at the top of the table
Department of Electronic & Electrical Engineering
Associativity
Consider the expression a ~ b ~ c.
• operator has left associativity, interpreted as (a ~ b) ~ c• right associativity, interpreted as a ~ (b ~ c)
Department of Electronic & Electrical Engineering
Associativity
Consider 8- 4+2
If associativity is left to right this is (8-4)+2 6 right to left 8-(4+2) 2
Department of Electronic & Electrical Engineering
Bitwise operators.
Operates at the level of individual bits of a type
Department of Electronic & Electrical Engineering
Examples : Bitwise operators
int a=5; /* 101 */int b=3; /* 011 */int c;
c= a & b ; /* bitwise AND c = 001 */c= a | b; /* bitwise OR c = 111 */c= a^b; /* XOR c = 110 */c= ~a; /* complement c = 1....11010 */c= a << 2 /* left shift by 2 c = 10100 */c= a >> 2 /* right shift 2 c = 001 */
Department of Electronic & Electrical Engineering
char x=2; /* 0000010 */char y=3; /* 0000011 */
char z=x && y; /* result is 1 */
char w=x & y; /* result is 2 */
printf(" %d %d \n",z,w);
bitwise versus logical operations