Friday, 4 November 2016

Operator C Language

Operator   C Language


Operator

In terms of programming an operator is a symbol which performs some specific task design by compiler. An Operator works on some operand which may be single or more than one. Operators, which work on single operand known as unary operator otherwise binary.
5+5 (five is operand and + is binary operator)
Following types of operators −
  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Misc Operators


Arithmetic Operators

Binary operator

An Operator takes two or more than two argument. Following table has list of available binary operator in c language.

Operator
Name
Description
Example
Output
+
Addition
Add two number
5+5
10
-
Subtraction
Subtract a number
10-5
5
*
Multiplication
Multiply by some number
5*6
30
/
Division
Divide a number
10/2
5
=
Assignment
Assign right hand side value
 to left hand
A=b

%
Mod
Return remainder by diving a
 number
17%3
2

Operator Priority 

Operator priority tells the compiler that which operator solve first if more than one operator exist in one expression.

Operator Priority Chart

Unary Operator

A Unary operator takes only one operand.

Operator
Name
Description
Example
Var_++
Postfix Increment
Increment By One
If appear in any expression then
First solve it with old value then increment
A++
++_Var
Prefix Increment
Increment By One
If appear in any expression then first increment
Variable value then solve expression

++A
a--
Postfix Decrement
Decrement By One
If appear in any expression then
First solve it with old value then Decrement
A--
--a
Prefix Decrement
Decrement By One
If appear in any expression then first Decrement
Variable value then solve expression

--A

If increment or decrement operator is not being part of any expression or equation then type is not important it will increase and decrement.
void main()
{
    int a,b=10;
    a=++b + ++b + ++b;
    printf("\n\tA=%d\n\tB=%d",a,b);
}

Output :
A=39
B=13

Assignment operator

An assignment operator transfer right hand side data after = to left hand side variable in overwrite mode. Overwrite means if left hand variable has some value then it will lost.

A=10;
B=A+10;

If any expression right hand side then first it use to solve and assign result to left side variable. In this example b will initialize by 20.

A=B=C=10;

Multiple assignments are allowed so all variable A, B and C will assign by 10;

Bitwise operator

A Bitwise operator works on a single bit not to entire byte.  Bitwise operators are similar to the logical operators, except that they work on a smaller scale.
  • ·         Working on bit in constraints of byte
  • ·         Increase speed synchronous to process clock pulse
  • ·         High level programming on bits
  • ·         Implement compression
  • ·         Encoding

A=5
B=6

Operator
Description
Example
&
Return true if both operand is 1 other wise 0
(A & B) = 4 i.e.,
101
110
100
Return true if any operand is 1 otherwise 0 in case of all operand is 0
(A B) = 7 i.e
101
110
111
^
Return true if operand is opposite otherwise 0.
(A ^ B) = 3 i.e.,
101
110
010
~
Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.
(~A ) = -6
<< 
Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. double the value of every move
A << 2 = 20 i.e.,
>> 
Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. half the value of every move
A >> 2 = 15 i.e., 0000 1111

void main()
{
     int a=5,b=6;
     clrscr();
     printf("\n\tBitwise and for %d & %d is %d",a,b,a&b);
     printf("\n\tBitwise or for %d | %d is %d",a,b,a|b);
     printf("\n\tBitwise xor for %d ^ %d is %d",a,b,a^b);
     printf("\n\tComplement of %d is %d",a,~a);
     printf("\n\tLeft Shift of %d by 1 is %d",a,a>>1);
     printf("\n\tRight Shift of %d by 1 is %d",a,a<<1);
     getch();

}

Download PDF

No comments:

Post a Comment