03 Operators
03 Operators
03 Operators
Course Leader:
Jishmi Jos Choondal
1
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Objectives
• At the end of this lecture, student will be able to
– Explain operators in C
– Use different types of operators
2
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Contents
• Operators
• Types of operators
• Operator precedence and associativity
3
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Operators and Operands
• Expressions consist of variables, operators and operands
a=b+5
• Operator: Symbol representing the operation
= and +
• Operand: The data items (variables and constant) on which the
operation is performed
a, b and 5
– In case of + operator, operands are b and 5
– In case of = operator, operands are a and the value of expression
b+5
4
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Operators – Types
• Operators are classified based on number of operands as
– Unary operators
– Binary operators
– Ternary operators
5
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Assignment Operator
• Assignment Operator =
a = 1;
1 is assigned to the variable a
6
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Arithmetic Operators
+ (addition)
a+5
- (subtraction)
10 - b
* (multiplication)
a*c
/ (division) – Integer division
a / 5 Example: 10/5 = 2 and 35/2 = 17
% (modulo) - Remainder
a%2 Example: 35%2 = 1
7
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Compound Assignment Operators
• Any statement of the form
variable = variable operator expression;
• Can be written as
variable operator = expression;
a = a + 1;
can be written as
a += 1;
8
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Increment and Decrement Operators
• Unary operators for increment and decrement
++
a++; /*post increment - Increment value of a after this
expression is evaluated*/
++a; /* pre increment - Increment value of a first and then
evaluate the expression */
--
a--; /* post decrement - Decrement value of a after this
expression is evaluated*/
--a; /* pre decrement - Decrement value of a first and then
evaluate the expression */
9
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Increment and Decrement Operators
• Let a=5 and b=10
a++; //a becomes 6
b--; //b becomes 9
• Example
int a,b,x=10,y=10;
a = x++; b = ++y;
printf("Value of a : %d",a);
printf("Value of b : %d",b);
• Applicable only to variables
– n++ //legal
– (i+j)++ //illegal
10
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Operators for Comparison
• Relational Operators
< Less than
> Greater than
!= Not equal to
== Is it equal to? (Do not confuse with assignment operator =)
<= Less than or equal to
>= Greater than or equal to
• Return
– 0 if false
– any other number if true (generally 1 is returned on true)
11
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Logical Operators
• Used to take decisions
&& logical AND
if ((a<b) && (a<c)) {}
|| logical OR
if ((a<b) || (a<c)) {}
! logical NOT (unary operator)
if(! ((a<b) && (a<c))) {}
• Return values
– 0 if false
– 1 if true (generally)
12
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Question
• How can we get the bits in memory?
13
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Bitwise Operators
•All data items are stored in computer’s memory as a
sequence of bits(0s and 1s)
& Bitwise AND The bits in the result are set to 1 if the
corresponding bits in the two operands are both 1 E.g., a&b;
| Bitwise OR The bits in the result are set to 1 if at least one of
the corresponding bits in the two operands is 1 E.g., a|b;
• It takes 3 operands
• Conditional expression
– Conditional operators with the operands
16
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Conditional Operators - Example
main(){
int num;
printf("Enter the Number : ");
scanf("%d",&num);
(num%2==0)?printf("Even"):printf("Odd");
}
17
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
sizeof() Operator
• sizeof() operators returns the size (number of bytes) of
the operand occupies
• Must precede its operand
• Operand may be a constant, a variable or a data type
• Syntax
sizeof(operand);
• Example
x=sizeof(int);
y=sizeof(x);
18
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Operators on Floating Point Data Type
• Operators that can be used on float and double data
types
– All Arithmetic operators except %(modulo) operator
– All Comparison operators
– Compound Assignment operators
– sizeof() operator
19
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Operator Precedence and Associativity
• Consider
a = 1+2*3/4;
• What is the value of a?
20
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Operator Precedence and Associativity
• To avoid confusion while reading, always use brackets
• The computer always gives one answer only as it
understands expressions based on precedence and
associativity
• Precedence
– Which operator to evaluate first
• Associativity
– Which operand to evaluate first
21
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Operator Precedence and Associativity
Further Reading
Kernighan, B. W. and Richie, D. (1992) The C Programming Language.
2nd ed., New Delhi:PHI.
25
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences