Expressions and Operators in C
Expressions in C
2
Expressions and Operators
c = a + b - 2;
3
Yes.
But I will tell you some other interesting things about them and other operators
I think I have already seen/used Arithmetic and Assignment operators in previous lectures/labs!
Arithmetic operators
4
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 | |
* | Multiplication | 9*2 is 18 | |
| | 9.1*2.0 is 18.2 | |
/ | Division | 9/2 is 4 | Integer division |
| | 9.1/2.0 is 4.55 | Real division |
% | Remainder | 9%2 is 1 | Only for int |
Unary operators
5
Operator | |
- | Negative of an expression |
++/-- | Increment/decrement a variable |
sizeof | Output memory box size for a variable |
type (examples: int, float, double, etc) | Type-casting |
Unary Operators - Negative
6
Unary operators – increment and decrement
7
int main(){
char a = ‘A’; float b = 3.31;
printf("%c\t%f\n",++a,b++);
printf("%c\t%f",--a,b--);
return 0;
}
B 3.31
A 4.31
Work with all data types
Note: The variable’s value will change for the rest of the program
Unary operators - sizeof
8
Unary operators - typecast
9
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;
}
Size is 8
Size is 1
C
Precedence Rules for Unary Operators
10
int main(){
int a = 1; int b = 2;
printf("%d", a + - + - b);
return 0;
}
3
-1
Bracket has the highest precedence
Note +x is x
Associativity Rules for Unary Operators
11
int main(){
int a = 1;
printf("%d", - ++a);
return 0;
}
-2
Relational Operators
12
Operator | Function |
> | Strictly greater than |
>= | Greater than or equal to |
< | Strictly less than |
<= | Less than or equal to |
== | Equal to |
!= | Not equal to |
Result is 0 or 1
1 means condition true, 0 means false
Relational Operators: Some Examples
13
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!
Assignment Operator
14
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 |
Precedence of Assign Operators
15
Operator Precedence
16
Example: a + b – c * d % e /f
Operators | Description | Associativity |
(unary) + - | Unary plus/minus | Right to left |
* / % | Multiply, divide, remainder | Left to right |
+ - | Add, subtract | Left to right |
< > >= <= | less, greater comparison | Left to right |
== != | Equal, not equal | Left to right |
= | Assignment | Right to left |
LOW
HIGH
(a+b) - (((c *d ) % e) / f)
Earlier the ASCII table. Now this table? Have to memorize this??
No.
Write it in your notebook
Logical Operators
17
Logical Op | Function | Allowed Types |
&& | Logical AND | char, int, float, double |
|| | Logical OR | char, int, float, double |
! | Logical NOT | char, int, float, double |
Logical Operators: Truth Table
18
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 |
“E” for expression
Logical Operators: Some Examples
19
Expr | 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 |
Logical Operators: Precedence and Associativity
20
1 && 0 || 1 || 0
0 || 1 || 0
1 || 0
1
2 == 2 && 3 == 1 || 1==1 || 5==4
Operator Precedence for various operators
21
Operators | Description | Associativity |
unary + unary - | Unary plus/minus | Right to left |
* / % | Multiply, divide, remainder | Left to right |
+ - | Add, subtract | Left to right |
< > >= <= | Relational operators | Left to right |
== != | Equal, not equal | Left to right |
&& | And | Left to right |
|| | Or | Left to right |
= | Assignment | Right to left |
INCREASING
LOW
HIGH
Note: Precedence of brackets () are above every other operator
Note: This list doesn’t include some other operators that we have not yet seen