Operators
in C :
"Operator is a symbol that is used to perform mathematical
operations."
When we use a variable in a program then we have to mention the
type of data. This can be handled using data type in C.
Followings are the most commonly used data types in C.
Operator Name
|
Operators
|
Assignment
|
=
|
Arithmetic
|
+, -, *, /, %
|
Logical
|
&&, ||, !
|
Relational
|
<, >, <=, >=, ==, !=
|
Shorthand
|
+=, -=, *=, /=, %=
|
Unary
|
++, --
|
Conditional
|
()?:;
|
Bitwise
|
&, |, ^, <<, >>, ~
|
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b;
clrscr();
a = 53;
printf("\n\t
Value of A : %d",a); // 53
b = a; // Interchange of value using assignment
printf("\n\n\t
Value of B : %d",b); // 53
getch();
}
It is also called as 'Binary operators'. It is used to perform
arithmetical operations. These operators operate on two operands.
#include <stdio.h>
#include <conio.h>
void main()
{
int
a,b,c,d,e,f,g;
clrscr();
printf("\n\t
Enter First Number :"); // 5
scanf("%d",&a);
printf("\n\t
Enter Second Number :"); // 2
scanf("%d",&b);
c = a + b;
printf("\n\n\t
Addition is : %d",c); // 7
d = a - b;
printf("\n\n\t
Subtraction is : %d",d); // 3
e = a * b;
printf("\n\n\t
Multiplication is : %d",e); // 10
f = a / b;
printf("\n\n\t
Division is : %d",f); // 2
g = a % b;
printf("\n\n\t
Modulus is : %d",g); // 1
getch();
}
Sometimes, we have to check more than one condition at a time
then it is operator which is primarily used to check more than two conditions.
This operator returns 1 if condition is true otherwise 0.
#include <stdio.h>
#include <conio.h>
void main()
{
int no1=2,
no2=5;
clrscr();
printf("\n\n
%d",(no1 && no2)); //
returns 1
printf("\n\n
%d",(no1 || no2)); // returns 1
getch();
}
It is also used to check conditions. These operators return 1 if
condition is true otherwise 0.
#include <stdio.h>
#include <conio.h>
void main()
{
int a=6, b=2;
clrscr();
printf("\n\n
A<=B : %d",(a<=b)); // 0 - False
printf("\n\n
A>B : %d",(a>b)); // 1 - True
printf("\n\n
A!=B : %d",(a!=b)); // 1 - True
getch();
}
It is used to perform
mathematical operations at which the result or output can affect on operands.
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b;
clrscr();
a = 18;
b = 4;
printf("\n\t
Value of A : %d",a); // 18
printf("\n\t
Using of B : %d",b); // 4
b += a ; // b = b + a
printf("\n\n\t
Using += (i.e b=b+a): %d",b); // 22
// Change the
operator as -=, *=, /=, %=
getch();
}
It operates on a single
operand. Therefore, this operator is called as 'unary operator.' It is used to
increase or decrease the value of variable by 1.
#include <stdio.h>
#include <conio.h>
void main()
{
int a=4, b;
clrscr();
printf("\n\n
Value of A : %d",a); // 4
a++; // Post
printf("\n\n
Value of A : %d",a); // 5
++a; // Pre
printf("\n\n
Value of A : %d",a); // 6
b=--a;
printf("\n\n
Value of A : %d",a); // 5
printf("\n\n
Value of B : %d",b); // 5
b=a++;
printf("\n\n
Value of A : %d",a); // 6
printf("\n\n
Value of B : %d",b); // 5
b++;
printf("\n\n
Value of B : %d",b); // 6
getch();
}
Conditional operator is
also called as 'ternary operator.' It is widely used to execute condition in
true part or in false part. It operates on three operands. The logical or
relational operator can be used to check conditions.
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b=3;
clrscr();
a = 5;
printf("\n\n
A is less than B ? ");
// No
getch();
}
8. Bitwise Operators
Bitwise operators are
used for manipulation of data at a bit level. They can be directly applied to
char, short int and long.
Operators
Precedence and Associativity :
In C, each and every operator has a spcial precedence which is
associated with it. There are various levels of precedence. This precedence is
especially used to determine to evaluation of expression which has more than
one operator in it. The operators which has higher precedence are executed
first and vice-versa. Operators which has same precedence level are evaluated
from left to right. It is dependant on it's level. This feature is well known
as 'Associativity of an operator.'
Associativity
|
Operator
|
Description
|
Left to Right
|
()
|
Function
|
[]
|
Array
|
|
-->
|
Pointer to member
|
|
.
|
Structure
|
|
|
||
Right to left
|
-
|
Unary Minus
|
+
|
Unary Plus
|
|
++ / --
|
Increment/Decrement
|
|
~
|
One's Complement
|
|
&
|
Address of
|
|
(type)
|
Type casting
|
|
sizeof
|
Size (in bytes)
|
|
!
|
Logical Not
|
|
*
|
Pointer reference
|
|
|
||
Left to Right
|
*
|
Multiplication
|
/
|
Division
|
|
%
|
Modulus
|
|
|
||
Left to Right
|
+
|
Addition
|
-
|
Subtraction
|
|
|
||
Left to Right
|
<<
|
Left Shift
|
>>
|
Right Shift
|
|
|
||
Left to Right
|
<
|
Less than
|
<=
|
Less than or equal to
|
|
>
|
Greater than
|
|
>=
|
Greater than or equal
to
|
|
|
||
Left to Right
|
==
|
Equality
|
!=
|
Not Equal to
|
|
|
||
Left to Right
|
&
|
Bitwise AND
|
|
||
Left to Right
|
^
|
Bitwise XOR
|
|
||
Left to Right
|
|
|
Bitwise OR
|
|
||
Left to Right
|
&&
|
Logical AND
|
|
||
Left to Right
|
||
|
Logical OR
|
|
||
Left to Right
|
? :
|
Conditional Operator
|
|
||
Right to Left
|
= *= +=
|
Assignment
|
|
||
Left to Right
|
,
|
Comma
|
No comments:
Post a Comment