03 Operators

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

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

• Do not confuse with equality Operator ==

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;

– Suppose a and b are 8-bit Integers


a has value 11010011
b has value 10101100
a&b ?
a|b ?
14
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Bitwise Operators contd.
! Complement All 0 bits are set to 1 and all 1 bits are set to 0
E.g., c = !a;
^ Bitwise XOR The bits in the result are set to 1 if exactly one of the
corresponding bits in the two operands is 1
E.g., c = a^b;
<< Left shift Shifts the bits of the first operand left by the number
of bits specified by the second operand; fill from the right with 0 bits
E.g., a<<2;
>> Right Shift Shifts the bits of the first operand right by the number
of bits specified by the second operand; the method of filling from
the left is machine dependent
E.g., a>>2;
15
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Conditional Operators
• Ternary operator ?:

• It has the general form


exp1 ? exp2 : exp3
exp1 is evaluated. If it is true, then exp2 is evaluated and becomes
the value of the expression. If exp1 is false, then exp3 is evaluated,
and that is the value

• 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

C Operator Precedence Table (DiFranco 2011)


22
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Example
• Consider • Refer to the operator
a = 1+2*(3/4); precedence chart when
writing expressions
Treated equal to a = 1+(2*(3/4) ); containing many
3/4 operators
2*0 • If you are uncertain
1+0 about the order of
1 evaluation in a complex
expression, use
a=1 parenthesis to group
expressions
23
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
Summary
• Expressions consist of
– operators, the symbols that represent an operation
– operands, the data items on which the operation is applied
• There are many types of operators
– Arithmetic
– Comparison
– Logical
– Bitwise
– Conditional, etc.,
• Expressions are evaluated based on precedence and
associativity of operators
24
Faculty of Engineering & Technology © Ramaiah University of Applied Sciences
References
DiFranco, D. (2011) C Operator Precedence Table, available at
http://www.difranco.net/compsci/C_Operator_Precedence_Table.h
tm (accessed 28 July 2014).

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

You might also like