Unit 1 - Operator (Part 3)
Unit 1 - Operator (Part 3)
Operator
• An operator is simply a symbol that is used to perform operations.
There can be many types of operations like arithmetic, logical,
bitwise, etc.
• There are following types of operators to perform different types of
operations in C language.
Unary operator
Arithmetic Operators
Relational Operators
Bitwise Operator
Logical Operators
Ternary or Conditional Operators
Assignment Operator
Comma Operator
Category Operator Associativity
Unary operator + - ! ~ ++ - - (type)* & sizeof() Right to left
Arithmetic Operators Left to right
* / %
+ - Left to right
Bitwise Shift Operators >> << Left to right
== != Left to right
Bitwise Operator &(Bitwise AND) Left to right
^(Bitwise XOR) Left to right
|(Bitwise OR)
Logical Operators &&(Logical AND) Left to right
include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
return 0;
}
Output:
Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte
Points to Remember
• Pre increment have highest priority.
• Post increment have lowest priority than all
the operator even from Assignment operator.
C Programming
Increment operators
Increment Operators:
C Programming
Syntax:
++ // increment operator
Examples:
a++;
++a;
C Programming
Type of Increment Operators
1. Pre Increment
2. Post Increment
C Programming
Pre Increment Operator (++variable):
Syntax:
++ variable;
C Programming
Pre Increment Operator:
b = ++a;
a=a+1;
b=a;
C Programming
void main()
{
int a,b;
a=3;
clrscr();
b=++a;
printf(“%d%d”,a,b);
getch();
}
C Programming
Post Increment Operator (variable++):
Syntax:
variable++;
C Programming
Post Increment Operator:
b = a++;
b=a;
a=a+1;
C Programming
void main()
{
int a,b;
a=3;
clrscr();
b=a++;
printf(“%d%d”,a,b);
getch();
}
C Programming
C Programming
Arithmetic Operators
Category Operator Associativity
Unary operator + - ! ~ ++ - - (type)* & sizeof() Right to left
Arithmetic Operators Left to right
* / %
+ - Left to right
Bitwise Shift Operators >> << Left to right
== != Left to right
Bitwise Operator &() Left to right
^(Bitwise XOR) Left to right
|(Bitwise OR)
Logical Operators &&(Logical AND) Left to right
C Programming
Precedence Rule for Arithmetic Operators
Precedence Rule for Arithmetic Operators:
1 stPriority: *, /, %
nd
2 Priority: +, -
C Programming
Addition (+):
operand1+operand2;
Example:
S=10+20;
C Programming
Subtraction (-):
operand1- operand2;
Example:
S=20-10;
C Programming
Multiplication (*):
operand1 * operand2;
Example:
M=5*10;
C Programming
Division (/):
operand1 / operand2;
Example:
10/2; answer: 5
7/3 answer: 2
3/7 answer: 0
C Programming
Operation Result
int/int = int
int/float = float
float/int = float
float/float = float
C Programming
Modulus Operator(%):
operand1 % operand2;
Example:
a=10%2;
C Programming
Arithmetic Instructions
Arithmetic Instructions:
C Programming
n= 2*3/4+5-6%3;
C Programming
n= 2*3/4+5-6%3;
n=6/4+5-6%3; // 2*3
n=1+5-6%3; // 6/4
n=1+5-0; // 6%3
n=6-0; // 1+5
n=6;
C Programming
C Programming
Relational operators
Category Operator Associativity
Unary operator + - ! ~ ++ - - (type)* & sizeof() Right to left
Arithmetic Operators Left to right
* / %
+ - Left to right
Bitwise Shift Operators >> << Left to right
== != Left to right
Bitwise Operator &(Bitwise AND) Left to right
^(Bitwise XOR) Left to right
|(Bitwise OR)
Logical Operators &&(Logical AND) Left to right
C Programming
Type of Relational Operators
1. Less than ( < )
2. Less than or equal to ( <= )
3. Greater than ( > )
4. Greater than or equal to ( >=)
5. Not equal to ( != )
6. Comparison Operator ( == )
C Programming
Precedence Rule for Relational Operators
Precedence Rule for Relational Operators:
2 nd Priority: == !=
C Programming
Less than ( < ):
C Programming
Less than ( < ):
void main()
{
int a,b,c;
a=4<5; // value of a = 1
b=14<5; // value of b = 0
c=5<5; // value of c = 0
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Less than ( < ):
void main()
{
int a,b,c,x;
x=4;
a=x<5; // value of a = 1
b=5<x; // value of b = 0
c=x<4; // value of c = 0
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Less than or equal to ( <= ):
C Programming
Less than or equal to ( <= ):
void main()
{
int a,b,c;
a=(4<=5); // value of a = 1
b=(14<=5); // value of b = 0
c=(5<=5); // value of c = 1
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Less than or equal to ( <= ):
void main()
{
int a,b,c,x;
x=4;
a=(x<=5); // value of a = 1
b=(5<=x); // value of b = 0
c=(x<=4); // value of c = 1
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Greater than ( > ):
C Programming
Greater than ( > ):
void main()
{
int a,b,c;
a= 4>5; // value of a = 0
b= 14>5; // value of b = 1
c= 5>5; // value of c = 0
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Greater than ( > ):
void main()
{
int a,b,c,x;
x=4;
a=x>5; // value of a = 0
b=5>x; // value of b = 1
c=x>4; // value of c = 0
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Greater than or equal to( >= ):
C Programming
Greater than or equal to( >= ):
void main()
{
int a,b,c;
a= (4>=5); // value of a = 0
b= (14>=5); // value of b = 1
c= (5>=5); // value of c = 1
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Greater than or equal to( >= ):
void main()
{
int a,b,c,x;
x=4;
a=(x>=5); // value of a = 0
b=(5>=x); // value of b = 1
c=(x>=4); // value of c = 1
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Comparison Operator or double equals to ( == ):
C Programming
Comparison Operator or double equals to ( == ):
void main()
{
int a, b, c;
a= (4==5); // value of a = 0
b= (14==5); // value of b = 0
c= (5==5); // value of c = 1
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Comparison Operator or double equals to ( == ):
void main()
{
int a,b,c,x;
x=4;
a=(x==5); // value of a = 0
b=(5==x); // value of b = 0
c=(x==4); // value of c = 1
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Not equal to ( != ):
C Programming
Not equal to ( != ):
void main()
{
int a, b, c;
a= (4!=5); // value of a = 1
b= (14!=5); // value of b = 1
c= (5!=5); // value of c = 0
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Not equal to ( != ):
void main()
{
int a,b,c,x;
x=4;
a=(x!=5); // value of a = 1
b=(5!=x); // value of b = 1
c=(x!=4); // value of c = 0
printf(“%d%d%d”,a,b,c);
getch();
}
C Programming
Example: Example: Example:
Output:
Output: 0 Output: 1
C Programming
Bitwise Operator in C
Category Operator Associativity
Unary operator + - ! ~ ++ - - (type)* & sizeof() Right to left
Arithmetic Operators Left to right
* / %
+ - Left to right
Bitwise Shift Operators >> << Left to right
== != Left to right
Bitwise Operator &(Bitwise AND) Left to right
^(Bitwise XOR) Left to right
|(Bitwise OR)
Logical Operators &&(Logical AND) Left to right
| Bitwise OR operator
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Bitwise AND operator
• Bitwise AND operator is denoted by the single ampersand sign (&). Two
integer operands are written on both sides of the (&) operator.
• If the corresponding bits of both the operands are 1, then the output of
the bitwise AND operation is 1; otherwise, the output would be 0.
For example,
• We have two variables a and b.
a =6;
b=4;
The binary representation of the above two variables are given below:
a = 0110
b = 0100
When we apply the bitwise AND operation in the above two variables, i.e.,
a&b, the output would be:
Result = 0100
Program using Bitwise AND operator
#include <stdio.h>
int main()
{
int a=6, b=14; // variable declarations
printf("The output of the Bitwise AND operator a&b is %d",a&b);
return 0;
}
#include <stdio.h>
int main()
{
int a=23,b=10; // variable declarations
printf("The output of the Bitwise OR operator a|b is %d",a|b);
return 0;
}
For example,
• If we have a variable named 'a',
a = 8;
The binary representation of the above variable is given below:
a = 1000
When we apply the bitwise complement operator to the operand, t
hen the output would be:
Result = 0111
Program using Bitwise complement
operator
#include <stdio.h>
int main()
{
int a=8; // variable declarations
printf("The output of the Bitwise complement operator ~a is %d",~a);
return 0;
}
• Output
Bitwise shift operators
Where,
Operand is an integer expression on which we apply the left-shift
operation.
n is the number of bits to be shifted.
• In the case of Left-shift operator, 'n' bits will be shifted on the left-side.
The 'n' bits on the left side will be popped out, and 'n' bits on the right-
side are filled with 0.
For example,
#include <stdio.h>
int main()
{
int a=5; // variable initialization
printf("The value of a<<2 is : %d ", a<<2);
return 0;
}
Output:
Right-shift operator
Where,
• Operand is an integer expression on which we apply the
right-shift operation.
• N is the number of bits to be shifted.
• In the case of the right-shift operator, 'n' bits will be shifted
on the right-side. The 'n' bits on the right-side will be
popped out, and 'n' bits on the left-side are filled with 0.
For example,
Output:
Logical Operator
Category Operator Associativity
Unary operator + - ! ~ ++ - - (type)* & sizeof() Right to left
Arithmetic Operators Left to right
* / %
+ - Left to right
Bitwise Shift Operators >> << Left to right
== != Left to right
Bitwise Operator &(Bitwise AND) Left to right
^(Bitwise XOR) Left to right
|(Bitwise OR)
Logical Operators &&(Logical AND) Left to right
Logical Operator
The Logical operators are used to perform logical operations
on the given expressions.
C Programming
An expression containing logical operator returns either 0 or
1 depending upon whether expression results true or false.
C Programming
Types of Logical Operators:
C Programming
Logical AND (&&):
A B A&&B
0 0 0
1 0 0
0 1 0
1 1 1
C Programming
Logical OR (||):
A B A||B
0 0 0
1 0 1
0 1 1
1 1 1
C Programming
Logical NOT (!):
A !A
0 1
1 0
C Programming
C Programming
Logical Instructions
The Logical operators are used to perform logical operations
on the given expressions.
C Programming
void main()
{
int a;
clrscr();
a=2||3&&4||0&&5||!7&&6;
printf(“%d”,a);
getch();
}
C Programming
Precedence Rule for Logical Operators:
1. Logical NOT ( ! )
2. Logical AND ( && )
3. Logical OR ( || )
C Programming
void main()
{
int a;
clrscr();
a=2||3&&4||0&&5||!7&&6;
printf(“%d”,a);
getch();
}
C Programming
a=2 || 3&&4 || 0&&5 || !7&&6;
a=2 || 3&&4 || 0&&5 || 0 &&6; // !7 = 0
a=2 || 1 || 0&&5 || 0 &&6; // 3&&4 = 1
a=2 || 1 || 0 || 0 &&6; // 0&&5 = 0
a=2 || 1 || 0 || 0 // 0&&6 = 0
a=1 || 0 || 0 // 2||1 = 1
a=1 || 0 // 1 || 0 = 1
a=1 // 1 || 0 = 1
C Programming
Example: Example: Example:
+ - Left to right
Bitwise Shift Operators >> << Left to right
== != Left to right
Bitwise Operator &(Bitwise AND) Left to right
^(Bitwise XOR) Left to right
|(Bitwise OR)
Logical Operators &&(Logical AND) Left to right
Assignment operators are used to assigning value to a variable. The left side operand of the assignment operator is a
variable and right side operand of the assignment operator is a value. The value on the right side must be of the same
data-type of the variable on the left side otherwise the compiler will raise an error.
Different types of assignment operators are shown below:
• “=”: This is the simplest assignment operator. This operator is used to assign the value on the right to the variable
on the left.
For example:
a = 10; b = 20;
ch = 'y';
• “+=”. This operator is combination of ‘+’ and ‘=’ operators. This operator first adds the current value of the
variable on left to the value on the right and then assigns the result to the variable on the left.
Example:
(a += b) can be written as (a = a + b)
• “-=”:This operator is combination of ‘-‘ and ‘=’ operators. This operator first subtracts the current value of the
variable on left from the value on the right and then assigns the result to the variable on the left. Example:
• (a -= b) can be written as (a = a - b)
• “*=”This operator is combination of ‘*’ and ‘=’ operators. This operator first multiplies the current value of the
variable on left to the value on the right and then assigns the result to the variable on the left. Example:
(a *= b) can be written as (a = a * b)
• “/=”This operator is combination of ‘/’ and ‘=’ operators. This operator first divides the current value of the
variable on left by the value on the right and then assigns the result to the variable on the left.
Example:
(a /= b) can be written as (a = a / b)
+ - Left to right
Bitwise Shift Operators >> << Left to right
== != Left to right
Bitwise Operator &(Bitwise AND) Left to right
^(Bitwise XOR) Left to right
|(Bitwise OR)
Logical Operators &&(Logical AND) Left to right
In the second statement, value of b will be 30, because 10, 20, 30 are
enclosed in braces, and braces has more priority than assignment (=)
operator. When multiple values are given with comma operator within the
braces, then right most value is considered as result of the expression.
Thus, 30 will be assigned to the variable b.
Example:
#include <stdio.h>
int main()
{
int a,b; Output
a = 10,20,30; a= 10, b= 30
b = (10,20,30);
//printing the values
printf("a= %d, b= %d\n",a,b);
return 0;
}
NOTE
• Try not to confuse between comma as a separator and
comma as an operator. Sample example:
int a = 4, 3;
• This will generate an error as comma in this case acts as a
separator as declaration takes place. So the error less code
will be as follows:
int a;
a = 4,3;
Now the value stored in a will be 4.
• Also, the following is valid,
int a =(4, 3);
here , 3 is stored in a.
Arrow operator
> An Arrow operator in C allows to access elements in Structures and Unions.
> It is used with a pointer variable pointing to a structure or union.
> The arrow operator is formed by using a minus sign, followed by the greater than
symbol as shown below.
Syntax:
(pointer_name)->(variable_name)
Operation: The -> operator in C or C++ gives the value held by variable_name to structure or
union variable pointer_name.
> The Dot(.) operator is used to normally access members of a structure or union.
> The Arrow(->) operator exists to access the members of the structure or the unions using
pointers.
Not equal to Relational Operator ( != )
Constant term cannot be placed on left hand side.
C Programming
Constant term can be placed in the left hand side.
C Programming
C Programming
Assignment Operator
vs
Comparison Operator
Assignment Operator:
For example:
int a = 10;
float b = 20.50;
char ch = 'y';
10 = a
C Programming
Comparison Operator:
For example:
C Programming