0% found this document useful (0 votes)
11 views22 pages

Lec 07

Uploaded by

anjandas0003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views22 pages

Lec 07

Uploaded by

anjandas0003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Expressions and Operators

in C
ESC111: Fundamentals of Computing I
Expressions in C pow is a function in math.h
(power function)
2
We use math formulae all the time in physics, chem,
x = y*y + z*z (= y 2
+ z 2
)
math
a=b/5
x=y*y+z*z
x = (int)(pow((double)y, 2.0) + pow((double)z, 2.0))
Mr C calls these formulae expressions
Oh! So two expressions
x = y * y + z * z is an expression for Mr Ccan be added together to
y * y + z * z is also an expression for Mr Cget another expression!
y * y is also an expression for Mr C So is z an
z * z isIt also
It sure sure an expression for Mr C expression?
Yes, take two expressions and do
is! is! operations like addition, multiplication,
or assignment (=) with them and a So is 5 an
new expression will emerge expression?
ESC101:
Fundamentals of
Expressions and Operators
 Expressions in C consist of one or more
variables/constants
 An expression contains one or more operators, such as
c = a + b - 2;
 Operators in C can be one
Yes. of the following type
But I will tell you
 Arithmetic some other I think I have
 Unary interesting things already seen/used

about them and Arithmetic and
Relational and logical
other operators Assignment
 Assignment operators in
 Conditional previous
lectures/labs! 3
Arithmetic operators
 Already seen. Operate on int, float, double (and
char) Op Meaning Example Remarks

+ Addition 9+2 is 11
9.1+2.0 is 11.1
- Subtraction 9-2 is 7
9.1-2.0 is 7.1
* Multiplicatio 9*2 is 18
n
9.1*2.0 is 18.2
/ Division 9/2 is 4 Integer
division
9.1/2.0 is 4.55 Real division
4
% Remainder 9%2 is 1 Only for int
Unary operators
Operator
- Negative of an expression

++/-- Increment/decrement a
variable
sizeof Output memory box size for a
variable
type (examples: int, float, Type-casting
double, etc)

5
Unary Operators - Negative
 Operators that take only one argument (or
operand)
 -5
 -b
 Observe that – is both an arithmetic and
unary operator
 Meaning depends on context
 This is called overloading

6
Unary operators – increment and decrement
 Increment (++) increases a variable by 1
 Decrement (--) decreases a variable by 1 Note: The variable’s
 ++variable is the pre-increment operator value will change for
the rest of the
 Means increment, then use program
 variable++ is the post-increment operator
 Means use, then increment Work with all
data types
 Likewise, the -- can be pre/post decrement
int main(){
char a = ‘A’; float b = 3.31;
printf("%c\t%f\n",++a,b++); B 3.31
A 4.31
printf("%c\t%f",--a,b--);
return 0;
}
7
Unary operators - sizeof
 Syntax
 sizeof var
 sizeof(type)
 Returns size of the operand in bytes
 sizeof(char) will return 1
 sizeof(float) will (mostly) return 4
 Very useful when you are porting programs
across computers
8
Unary operators - typecast
 Syntax
 (type) var, for example – (int) a, (float) a, etc
 We have already seen this Size is 8
 What will be the output of this program? Size is 1
C
int main(){
double a = 67.2;
printf("size is %d\n", sizeof a);
printf("size is %d\n", sizeof((char) a));
printf("%c", (char) a);
return 0;
} 9
Precedence Rules for Unary Operators
 Precedence rules tell us the order in which the
operators will be applied in any C expression
 Unary ops are above arithmetic ops, only below Bracket has
brackets the highest
 If a is 1 and b is 2, what will a + -b be evaluated
precedence
-1
as?
3
int main(){
 What int aabout
= 1; intthis
b = 2;program?
printf("%d", a + - + - b);
return 0;
}
Note +x is10x
Associativity Rules for Unary Operators
 Associativity rules tell us how the operators of
same precedence are grouped (e.g., a+b+c will be
evaluated as (a+b)+c, not a+(b+c))
 For unary operators, the associativity is from right
to left
 Important to remember this
 Most other operators’ associativity is left to-2right (e.g., +
int main(){
operator)
int a = 1;
 What will this program print?
printf("%d", - ++a);
return 0;
} 11
Relational Operators
 Compare two quantities
Operator Function Result is
> Strictly greater than 0 or 1
>= Greater than or equal to
< Strictly less than
<= Less than or equal to
== Equal to
!= Not equal to 1 means
condition
true, 0
 Work on int, char, float, double… means
false
12
Relational Operators: Some Examples
Rel. Expr. Result Remark
3>2 1
3>3 0
‘z’ > ‘a’ 1 ASCII values used for char
2 == 3 0
‘A’ <= 65 1 'A' has ASCII value 65
‘A’ == ‘a’ 0 Different ASCII values
(‘a’ – 32) == ‘A’ 1
5 != 10 1
1.0 == 1 AVOID May give unexpected result due to
approximation

Avoid mixing int and float values while comparing.


Comparison with floats is not exact! 13
Relational Operators: Another
Example
 Problem: Input 3 positive integers. Print the count
of inputs that are even and odd.
INPUT
 Do not use if-then-else OUTPUT
10
Even=1
5 Odd=2
int a; int b; int c; 3
int cEven; // count of even inputs
scanf(“%d%d%d”, &a,&b,&c); // input a,b,c

// (x%2 == 0) evaluates to 1 if x is Even,


// 0 if x is Odd
cEven = (a%2 == 0) + (b%2 == 0) + (c%2 == 0);
printf(“Even=%d\nOdd=%d”, cEven, 3-cEven);
14
Assignment Operator
• Basic assignment (variable = expression)

Variant Meaning
Var += a Var = Var + a
Var -= a Var = Var – a
Var *=a Var = Var *a
Var /=a Var = Var/a
Var %=a Var = Var%a

15
Precedence of Assign Operators
 Always the last to be evaluated
 x *= -2 *(y+z)/3
 x = x*(-2*(y+z)/3)
 Seldom need to worry about it

16
Earlier the ASCII table.
Operator Precedence Now this table? Have to
memorize this??
Operators Description Associativity

HIGH (unary) + - Unary plus/minus Right to left


*/% Multiply, divide, remainder Left to right
+- Add, subtract Left to right No.
< > >= <= less, greater comparison Left to right Write it in
== != Equal, not equal Left to right
your
= Assignment Right to left
LOW notebook

Example: a + b – c * d % e /f
(a+b) - (((c *d ) % e) / f)

17
Logical Operators
Logical Op Function Allowed Types

&& Logical AND char, int, float, double


|| Logical OR char, int, float, double
! Logical NOT char, int, float, double
 Remember
 value 0 represents false.
 any other value represents true.
Compiler returns 1 by default
18
Logical Operators: Truth Table
“E” for
expression E1 E2 E1 && E2 E1 || E2
0 0 0 0
0 Non-0 0 1
Non-0 0 0 1
Non-0 Non-0 1 1

E !E
0 1
Non-0 0
19
Logical Operators: Some
ExamplesExpr Result Remark
2 && 3 1
2 || 0 1
‘A’ && ‘0’ 1 ASCII value of ‘0’≠0
‘A’ && 0 0
‘A’ && ‘b’ 1
! 0.0 1 0.0 == 0 is guaranteed
! 10.05 0 Any real ≠ 0.0
(2<5) && (6>5) 1 Compound expr

20
Logical Operators: Precedence and
Associativity
 NOT has same precedence as equality operator
 AND and OR are lower than relational operators
 OR has lower precedence than AND
 Associativity goes left to right
2 == 2 && 3 == 1 || 1==1 || 5==4

1 && 0 || 1 || 0

0 || 1 || 0 1 || 0 1 21
Operator Precedence for various operators
Note: Precedence of brackets () are above every other operator
Operators Description Associativity Note: This list
unary + unary - Unary plus/minus Right to left doesn’t include
HIGH some other
*/% Multiply, divide, remainder Left to right operators that we
I have not yet seen
N +- Add, subtract Left to right
C < > >= <= Relational operators Left to right
R
E == != Equal, not equal Left to right
A
S && And Left to right
I
N || Or Left to right
G
= Assignment Right to left
LOW
22

You might also like