Module 2 - Part B - Operators and Expressions
Module 2 - Part B - Operators and Expressions
MODULE 2
Expressions
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 (<,>,<=,>=,!=,==)
v op = exp ;
Short Hand Assignment Operators
Increment and Decrement Operators (++ , --)
a = ++x;
x = 11
x = 10 a = ++x ; a = 11
Post-increment operator
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
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.
a = 10;
b = 15;
x = (a > b) ? a : b ;
Special Operators
1) Comma Operator :
Comma operators are used to link related expressions together.
(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
x = 34 + 12 / 4 - 45