1) Lec3 Operators PDF
1) Lec3 Operators PDF
Operators in C
Joy Mukherjee
1 / 23
Outline
1 Expressions
2 Operators
3 Typecasting
4 Type Promotion
2 / 23
Outline
1 Expressions
2 Operators
3 Typecasting
4 Type Promotion
3 / 23
Expressions
4 / 23
Order of Evaluation
5 / 23
Outline
1 Expressions
2 Operators
3 Typecasting
4 Type Promotion
6 / 23
Types of Operators
Arithmetic + - * / % ++ - -
Relational < > <= >= == !=
Bitwise & |
Logical && || !
Ternary ?:
Assignment = += -= *= /= %=
Comma ,
7 / 23
Arithmetic Operators
C does not provide a built-in exponentiation operator.
Operator Meaning Description
- unary Applicable for integers and real numbers.
negation Makes no sense for unsigned operands.
+ binary ad- Integers and real numbers.
dition
- binary Integers and real numbers.
subtrac-
tion
* binary Integers and real numbers.
multipli-
cation
/ binary di- If both are integers division means quo-
vision tient, otherwise for real numbers division
means real division.
% binary re- Integer operands.
mainder
Table: Arithmetic Operators 8 / 23
Post-increment and Pre-increment Operators
int a = 3, b, c;
9 / 23
Bitwise Operators
10 / 23
Bitwise Operators
11 / 23
Relational Operators
12 / 23
Relational Operators: Example
int x = 15, y = 40;
At that time, the following truth values hold:
x == y False
x! = y True
y %x == 10 True
600 < x y False
600 <= x y True
B > A True
x/0.3 == 50 False (due to floating point errors)
Table: Examples of Relational Operators
Note that the equality checker is == and not the single =. Recall that =
is the assignment operator. In a place where a relational condition is
expected, an assignment of the form E1 = E2 makes sense and could be a
potential source of bugs.
13 / 23
Logical Operators
14 / 23
Logical Operators: Example
Note that here is yet another source of logical bug. Using a single & and |
in order to denote a logical operator actually means letting the program
perform a bitwise operation and possibly ending up in a logically incorrect
answer.
15 / 23
The ? Operator
16 / 23
The Comma Operator
17 / 23
Summary of Precedence and Associativity
18 / 23
Outline
1 Expressions
2 Operators
3 Typecasting
4 Type Promotion
19 / 23
Typecasting
20 / 23
Typecasting: Example
1 Expressions
2 Operators
3 Typecasting
4 Type Promotion
22 / 23
Type Promotion
23 / 23