Explain different operators of C language with example.
Answer:
Operator: It is a symbol which is
used to tell the compiler which mathematical or logical operations are to be
applied on operands (data).
C language
has following 8 different types of operators.
1)
Arithmetic
operators
2)
Relational
operators
3)
Logical
operators
4)
Assignment
operators
5)
Increment
or decrement operators
6)
Bit
wise operators
7)
Ternary
(Conditional) operators
8)
Other
operators
Arithmetic operators:
It is used to perform
arithmetic operation like addition, subtraction, division, multiplication and
modules.
Operator
Name
|
Meaning
|
+
|
Addition
|
-
|
Subtraction
|
/
|
Division
|
*
|
Multiplication
|
%
|
Modulus
|
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
a=10,b=20;
clrscr();
c=a+b;
printf(“%d”,c);
getch();
}
Relational Operator:
These operators are used to find the
relationship between two operands.
The answers of these operators are
always either true or false.
Consider a = 10 and b = 5 for
following table.
Operator
|
Meaning
|
Answer
|
a==b
|
Check the value of a and b are same
or not
|
False
|
a>b
|
Check the value of a is greater
than the value of b
|
True
|
a<b
|
Check the value of a is less
than the value of b
|
False
|
a>=b
|
Check the value of a is greater
than or equals to the value of b
|
True
|
a<=b
|
Check the value of a is less
than or equals to the value of b
|
False
|
a!=b
|
Check the value of a is not
equal to the value of b
|
True
|
Logical Operators:
Logical operators are
used to combine the more than one condition.
C language has 3
different types of logical operators.
The output of logical
operators is always either true or false.
Consider a = 10, b = 5
and c = 3 for following table.
Operator
|
Example
|
Output
|
&& - Logical and
|
(a > b) && (a>c)
|
True
|
|| - Logical or
|
(b > a) || (a>b)
|
True
|
! – Logical not
|
!(a>b)
|
False
|
Assignment Operators:
Assignment operator is used to assign
the value to variable.
To assign the value to variable
following syntax is used.
Syntax:
<Variable
name> = <expression>;
Expression can be any constant,
variable name or any valid expression.
Example: a = 10;
C language also support shorthand
operator.
Shorthand operator can be used with
statement having following form:
<Variable
name> = <variable name> <operator> <expression>;
Here both variable names must be
same.
This form is converted into following
form using shorthand operator.
<Variable
name> <operator> = <expression>;
Assignment
operator
|
Shorthand
|
a = a + 5;
|
a+=5;
|
a = a – 5;
|
a-=5;
|
a = a *
10;
|
a*=10;
|
a = a /
20;
|
a/=20;
|
Increment or Decrement operator:
It is unary operator that means it
requires only one operator.
Operator can be written either after
or before the operand.
Operator
|
Meaning
|
++
|
Increment operator
|
--
|
Decrement operator
|
Increment operator is
used to increase the value of variable by one and decrement operators is used
to decrease the value of variable bye one only.
If increment operator
is placed after variable then it is known as post increment and if increment
operator is placed before variable then it is known as pre increment.
Example:
#include<stdio.h>
#include<conio.h>
void
main()
int
a1,b1;
a1=10;
b1=a1++;
// post increment
printf(“%d”,b1);
printf(“%d”,a1);
getch();
}
Bitwise operator:
C language also supports some
operators which can perform operation on bit level. So using this operator we
can perform operation on 0’s and 1’s bit.
Consider a = 10 (1010) and b = 5
(0101).
Operator
|
Example
|
Output
|
& -
Bitwise AND
|
c = a
& b
|
0
|
| -
Bitwise OR
|
c = a | b
|
15
|
^ -
Bitwise Exclusive OR (XOR)
|
c = a ^ b
|
15
|
<< -
Left Shift
|
c = a
<< 1
|
20
|
>> -
Right Shift
|
c = a
>> 1
|
5
|
~ -
Bitwise 1’s complement
|
c = ~a
|
-11
|
For Bitwise and (&) operator if
both bits are 1 then only answer is 1.
For Bitwise or(|) operator if one the
bit is 1 then answer is 1 and if both bits are 0 then only answer is 0.
For Bitwise exclusive or (XOR)
operator if both bits are different then only answer is 1 other wise answer is
0.
In left shift operator if we shift
one bit to left then result will be in multiplication of 2, if we shift 2 bits
in left side then result will be in multiplication of 4 and so on.
For Example:
a=10; if we write a<<1 then
answer is 20 as below.
0
|
0
|
0
|
0
|
1
|
0
|
1
|
0
|
0
|
0
|
0
|
1
|
0
|
1
|
0
|
0
|
Example:
#include<stdio.h>
#include<conio.h>
void
main()
int
a1,b1;
a1=10;
b1=a1<<1;
printf(“%d”,b1);
b1=a1>>1;
printf(“%d”,b1);
getch();
}
Conditional operator:
It is also known as ternary operator.
It works like if else statement.
|
If
condition becomes true then statement 1 will be executed.
If condition becomes false then statement 2 will be
executed
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int max,a,b;
clrscr();
a=10, b=20;
max = (a>b)? a : b;
printf(“maximum value is
%d”,max);
getch();
}
Other operators:
In that some special
operators like comma operator, dot (.) operator, arrow (->)
operator and sizeof operator are used.
Comma
operator is used to declare multiple variable, expression and also used in
printf() or scanf() function.
Dot
(.) operator is used to access the member of structure using structure
variable.
Arrow
(->) operator is used to access the member of structure using pointer
variable.
Sizeof() operator is used to find the no of bytes occupied by specified data type or
variable.
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf(“%d”,sizeof(a)); //
size of integer is 2 bytes so output is 2
getch();
}
No comments:
Post a Comment