You are on page 1of 24

OPERATORS AND EXPRESSIONS

MODULE 2
Expressions

 Combination of operators and operands which reduces to a single value.


 An operation is performed on a data item which is called an operand.
 An operator indicates an operation to be performed on data.

z = 3+2*1
z=5
C Operators
C programming language supports a rich set of operators that are classified as
follows.
► Arithmetic Operators
► Relational Operators
► Logical Operators
► Assignment Operators
► Increment & Decrement Operators
► Bitwise Operators
► Conditional Operator
► Special Operators
Arithmetic Operators(+,-,*,/,%)
Types of Arithmetic Operators
 Integer Arithmetic
a + b = 18
 Real Arithmetic
x = 1.0/3.0
= 0.33333333
 Mixed mode Arithmetic
15/10.0 = 1.5
15/10 = 1
Relational Operators (<,>,<=,>=,!=,==)

ae-1 relational operator ae-2


Logical Operators (&&,||,!)
Assignment Operator(=)

v op = exp ;
Short Hand Assignment Operators
Increment and Decrement Operators (++ , --)

 increment ++ and decrement -- to change the value of an operand


(constant or variable) by 1.
 Increment ++ increases the value by 1 whereas decrement -- decreases
the value by 1.
 These two operators are unary operators, meaning they only operate
on a single operand.
Pre-increment and Post-increment

 Pre-increment operator: Increment the value of a variable before using it in a


expression.
 In the Pre-Increment, value is first incremented and then used inside the
expression.

a = ++x;

x = 11
x = 10 a = ++x ; a = 11
Post-increment operator

 Post-increment operator : Increment the value of variable after executing


expression completely in which post increment is used.
 In the Post-Increment, value is first used in a expression and then
incremented. ► Syntax: ► Here, suppose the value of ‘x’ is 10 then value of
variable ‘b’ will be 10 because old value of ‘x’ is used.

a = x++;
x = 11
x = 10 a = x++; a = 10
Bitwise Operators
Bitwise AND Operator(&)
 The output of bitwise AND is 1 if the corresponding bits of two
operands is 1.
 If either bit of an operand is 0, the result of corresponding bit is
evaluated to 0.
 Let us suppose the bitwise AND operation of two integers 12 and 25.
Bitwise OR Operator(|)
 The output of bitwise OR is 1 if atleast one of the corresponding bit
of two operands is 1.
 Let us suppose the bitwise OR operation of two integers 12 and 25.
Bitwise XOR Operator(^)
 The result of bitwise exclusive operator is 1 if the corresponding bits
of two operands are opposite.
 .Let us suppose the bitwise XOR operation of two integers 12 and 25.
Shift Operators

 There are two shift operators in C programming:


► Right shift operator
► Left shift operator.

Right shift operator shifts all bits towards right by certain number of
specified bits. It is denoted by >>.
 Left shift operator shifts all bits towards left by a certain number of
specified bits.
 The bit positions that have been vacated by the left shift operator are
filled with 0. The symbol of the left shift operator is <<
Conditional Operator(? :)
 Decision-making statements that depend upon the output of the expression.
 As a conditional operator works on three operands, so it is also known as
the ternary operator.
 It starts with a condition, hence it is called a conditional operator.
 Conditional operators return one value if condition is true and returns
another value is condition is false.

expression1 ? expression2 : expression3;

a = 10;
b = 15;
x = (a > b) ? a : b ;
Special Operators
1) Comma Operator :
Comma operators are used to link related expressions together.

2) The sizeof operator :


Returns the size of data (constants, variables, array, structure, etc).
Dot and Arrow operator

(Type) operator
• The type conversion performed by the programmer by posing the data type of the
expression of specific type is known as explicit type conversion.
• The explicit type conversion is also known as type casting.
Example:
Operator precedence

 Determines the grouping of terms in an expression and decides


how an expression is evaluated.
 Certain operators have higher precedence than others

x = 34 + 12 / 4 - 45

You might also like