0% found this document useful (0 votes)
11 views

02 Module2 Operators

Uploaded by

gowravgowrav1697
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

02 Module2 Operators

Uploaded by

gowravgowrav1697
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Module 2

Chapter 9.15 and 9.16


Operators in C
An operator is a symbol that specifies the mathematical, logical, or relational operation to be performed.
The different operators supported in ‘C’ are:
 Arithmetic Operators
 Relational Operators
 Equality Operators
 Logical Operators
 Unary Operators
 Conditional Operator
 Bitwise Operator
 Assignment Operators
 Comma Operator
 sizeof Operator

Arithmetic Operators: The different arithmetic operators are:


Operator Name Result Syntax Example (b=5, c=2)
+ Addition Sum b+c 7
- Subtraction Difference b-c 3
* Multiplication Product b*c 10
/ Division Quotient b/c 2
% Modulus Remainder b% c 1

Relational Operators: These are used to compare two quantities. The output will be either 0
(False) or 1(True). The different relational operators are:
Operator Name Syntax Example
(b=5, c=2)
< Lesser than b<c 0 (False)
> Greater than b>c 1 (True)
<= Lesser than or Equal to b <= c 0 (False)
>= Greater than or Equal to b >= c 1 (True)

Equality Operators:

== operator returns 1, if both the operands are same, otherwise returns 0.


!= operator returns 1,if both the operands are different, otherwise returns 0.

1
Operator Name Syntax Example (b=5, c=2)
== Equal to b == c a = 0 (False)
!= Not equal to b!= c a = 1 (True)

Logical Operators: C language supports three logical operators- logical AND (&&), logical
OR (||), and logical NOT (!). These are used to test more than one condition and make
decision.
Logical AND (&&) The output is true only if both inputs are true. It accepts two or more
inputs.
Input Output
X Y X && Y
0 0 0
0 1 0
1 0 0
1 1 1

Logical OR (||) The output is true only if any of its input is true. It accepts two or more
inputs.
Input Output
X Y X || Y
0 0 0
0 1 1
1 0 1
1 1 1

Logical NOT (!) The output is true when input is false and vice versa. It accepts only
one input.
Input Output
X !X
0 1
1 0

2
Unary Operators: Unary operator acts on single operands. C language supports three
unary operators-unary minus, increment and decrement operators.
When an operand is preceded by a minus sign, the unary operator negates its value.
For example:
int a, b = 10;
a= -(b)
then result of the expression is a=-10

Increment (+ +): It adds one to the operand.


Pre-increment (prefix) Post-increment (postfix)
First value of the operand is First value of the operand is used
incremented for evaluationthen, it is
incremented (added) by 1.
(added) by 1 then, it is used for
evaluation.
Ex: ++a Ex: a++

Decrement (- -): It subtracts one from the operand.


Pre-decrement (prefix) Post- decrement (postfix)
First value of the operand is First value of the operand is used
decremented(subtracted) by 1 then, forevaluation then, it is
it is used for evaluation. decremented (subtracted) by 1.
Ex: - -a Ex: a- -

For example:
int x = 10, y;
y = x++;
is equivalent to writing
y=x;
x=x+1;

whereas,
y = ++x;
is equivalent to writing
x=x+1;
y=x;

Conditional Operators: It is also known as ternary operator. The syntax of conditional


operator:
exp1? exp2: exp3

3
exp1 is evaluated first. If it is true, then exp2 is evaluated and becomes the result of the
expression, otherwise exp3 is evaluated and becomes the result of the expression.
For example,
large = (a> b)? a: b
First exp1, that is (a > b) is evaluated. If a is greater than b, then large = a, else large = b.
Hence, large is equal to either a or b but not both

Bitwise Operators:
Bitwise operators are those operators that perform operations at bit level. These operators
include: bitwise AND, bitwise OR, bitwise XOR, and shift operators.
Bitwise AND (&), Bitwise OR (|), Bitwise XOR (^)
X Y X&Y X|Y X^Y
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0

Bitwise NOT (~)


X ~X
0 1
1 0

Shift Operators
C supports two bitwise shift operators. They are shift-left (<<) and shift right (>>). The syntax
for a shift operation can be given as:
operand op num
where the bits in operand are shifted left or right depending on the operator.
For example, if x = 0001 1101, then x << 1 produces 0011 1010
When operator is shift left, then MSB of x will be lost and LSB are set to 0.
For example, if x = 0001 1101, then x >>1 produces 0000 1110
When operator is shift right, then LSB of x will be lost and MSB are set to 0.

Assignment Operators:
These are used to assign the result or values to a variable. The different types of assignment
operators are:

4
Simple Assignment a = 10
Shorthand Assignment a += 10 equal to a = a + 10
Multiple Assignment a = b = c = 10

Comma Operator: It can be used as operator in expression and as separator in


declaring variables.
For example:
int a=2, b=3, x=0;
x= (++a, b+=a) . The value of x=6 after the evaluation

Sizeof Operator: It is used to determine the size of variable or value in bytes. For ex:
int a = 10;
unsigned int result;
result = sizeof(a);

Example of C program to illustrate all the operators


#include <stdio.h>
int main()
{
int a=18,b=2;
printf("%d %d %d %d %d\n",a+b,a-b,a*b,a/b,a%b); //arithmetic operators
printf("%d %d %d %d\n",a<b,a>b,a>=b,a<=b);//Relational operators
printf("%d %d\n",a==b,a!=b);//Equality operators
printf("%d %d %d\n",(a<b)&&(b<a),(a<b)||(b<a),!(a<b));//Logical operators
printf("%d %d\n",b++,++b);//Unary increment operator
printf("%d %d\n",a--,--a);//Unary Decrement operator
printf("%d\n",(a<b?a:b));//conditional opeartor
printf("%d %d %d %d %d %d\n",a&b,a|b,a^b,~a,a<<1,a>>1);//Bitwise operators
printf("%d\n",(b++,b+=4));//comma operator
printf("%ld",sizeof(char));
return 0;
}

Output:
20 16 36 9 0
0110
01
011
34
17 16
4
0 20 20 -17 32 8
9
1

5
Evaluate the following expressions step by step
1. A=x*2+y/5-z*y where x=3, y=5, z=7
A=x*2+y/5-z*y
=3*2+5/5-7*5
=6+5/5-7*5
=6+1-7*5
=6+1-35
=7-35
=-31
2. A=++x*(y-3)/2-z++*y where x=3, y=5, z=7
A=++x*(y-3)/2-z++*y
=++x*(5-3)/2-z++*5
=++x*2/2-7*5
=4*2/2-7*5
=8/2-7*5
=4-7*5
=4-35
=-31

Operator Precedence Chart


C operators have two properties: priority and associativity. Associativity defines the
direction in which the operator acts on the operands. It can be either left-to-right or right-
to-left. Priority is given precedence over associativity to determine the order in which the
expressions are evaluated. The below table lists the operators that C language supports
in the order of their precedence (highest to lowest).

Table: Operator Precedence


Operator Associativity Operator Associativity
() left to right & left to right
[]
.

++(postfix) right to left ^ left to right
--(postfix)
++(prefix) right to left | left to right
--(prefix)

6
+(unary)
-(unary)
!
~
(type)
*(indirec
tion)
&(addres
s)
sizeof
*/ % left to right && left to right
+ - left to right || left to right
<< >> left to right ?: right to left
< <= left to right = right to left
> >= += -=
*= /=
%= &=
^= |=
<<=
>>=
== != left to right (comma) left to right

Type Conversion and Typecasting


Type conversion is done when the expression has variables of different data types. To evaluate
the expression, the data type is promoted from lower to higher level where the hierarchy of data
types (from higher to lower) can be given as: double, float, long, int, short, and char. Below
Figure depicts type conversions in an expression:

7
Typecasting: It is also known as forced conversion. Typecasting an arithmetic
expression tells the compiler to represent the value of the expression in a certain way.
For ex:
float salary = 10000.00;
int sal;
sal =(int) salary;
In this example floating point numbers are converted to integer by typecasting.

You might also like