Tuesday, November 07, 2017

Operators in C

Operators in C:

An operator is simply a symbol which takes one or more operands or expressions and performs operations.
¾     Operands are variables or expressions which are used in conjunction with operators to evaluate the expression.
¾     If an operator requires one operand, it is called as Unary Operator,
¾     If an operator requires two operands, then it is called as Binary Operator and if it requires three operands, then it is called as ternary operator/conditional operator.
¾     Combination of operands and operators form an expression.
¾     Expressions are sequences of operators, operands, and punctuators that specify a computation.
¾     There are following types of operators to perform different types of operations in C language.


1.      Arithmetic Operators
2.      Relational Operators
3.      Logical Operators
4.      Bitwise Operators
5.      Ternary or Conditional Operators
6.      Assignment Operator
7.      Selection Operator
8.      Misc Operator

I) Unary Operators

Operator
Name
Operator
Name
!
Logical NOT
++
Increment by 1
&
Address-of
Unary negation
( )
Cast Operator
––
Decrement by 1
*
Pointer dereference
~
Complement
+
Unary Plus




II) Binary Operators


Operator
Name
Operator
Name
,
Comma
/=
Division assignment
!=
Inequality
Less than
%
Modulus
<< 
Left shift
%=
Modulus assignment
<<=
Left shift assignment
&
Bitwise AND
<=
Less than or equal to
&&
Logical AND
=
Assignment
&=
Bitwise AND assignment
==
Equality
*
Multiplication
Greater than
*=
Multiplication assignment
>=
Greater than or equal to
+
Addition
>> 
Right shift
+=
Addition assignment
>>=
Right shift assignment
Subtraction
^
Exclusive OR
–=
Subtraction assignment
^=
Exclusive OR assignment
–>
Member selection
|
Bitwise inclusive OR
–>*
Pointer-to-member selection
|=
Bitwise inclusive OR assignment
/
Division
||
Logical OR

All Binary operators are discussed later in this topic only.

1) Arithmetic Operators

            There are following arithmetic operators supported by C language. Assume variable ‘A’ holds 10 and variable ‘B’ holds 20 then:

Operator
Description
Example
+
Adds two operands
A + B will give 30
-
Subtracts the second operand from the first
A - B will give -10
*
Multiply both operands
A * B will give 200
/
Divide numerator by the denominator
B / A will give 2
%
Modulus Operator and a remainder of after an integer division
B % A will give 0
++
Increment operator, increases integer value by one
A++ will give 11
--
Decrement operator, decreases integer value by one 
A-- will give 9

Program: WAP to demonstrate arithmetic operators.

#include<stdio.h>
#include<conio.h>
void main()
   int a = 21,b = 10, c;
   c = a + b;
   printf("Line 1 - Value of c is %d\n", c );
   c = a - b;
   printf("Line 2 - Value of c is %d\n", c );
   c = a * b;
   printf("Line 3 - Value of c is %d\n", c );
   c = a / b;
   printf("Line 4 - Value of c is %d\n", c );
   c = a % b;
   printf("Line 5 - Value of c is %d\n", c );
   c = a++;
   printf("Line 6 - Value of c is %d\n", c );
   c = a--;
   printf("Line 7 - Value of c is %d\n", c );
   getch();
}

Output
Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
Line 6 - Value of c is 21
Line 7 - Value of c is 22

There are two types of Increment / Decrement Operators, first one is pre increment/decrement and the second is post increment/decrement. 

i) Pre-Increment:

            In pre-increment, the value is first incremented and then used in the expression.

Program: WAP to demonstrate Pre-Increment Operator.

#include<stdio.h>
#include<conio.h>
void main()
   int a = 10,b = 2, c;
   c = ++a + b; // ++a is pre-increment and now value of a is 11.
   printf("Value of c is %d\n", c );
  getch();
}

Output
Value of c is 13

ii) Pre-Decrement:

            In pre-decrement, the value is first decremented and then used in the expression.


Program : WAP to demonstrate Pre-Decrement Operator. 


#include<stdio.h>
#include<conio.h>
void main()
   int a = 10,b = 2, c;
   c = --a + b; // --a is pre-decrement and now value of a is 9.
   printf("Value of c is %d\n", c );
  getch();
}

Output
Value of c is 11

 iii) Post-Increment:

            In post-increment, the expression is executed first and then the value of the variable is incremented.

Program : WAP to demonstrate Post-Increment Operator.


#include<stdio.h>
#include<conio.h>
void main()
   int a = 10,b = 2, c;
   c = a++ + b; // a++ is post-increment and value of a is 10 (no change).
   printf("Value of c is %d\n", c );
   printf("Value of a is %d\n", a ); //value of a is 11
  getch();
}


Output
Value of c is 12
Value of a is 11 

iv) Post-Decrement:

            In post-decrement, the expression is executed first and then the value of the variable is decremented.

Program : WAP to demonstrate Post-Decrement Operator.

#include<stdio.h>
#include<conio.h>
void main()
   int a = 10,b = 2, c;
   c = a-- + b; // a-- is post-decrement and value of a is 10 (no change).
   printf("Value of c is %d\n", c );
   printf("Value of a is %d\n", a ); //value of a is 9
  getch();
}

Output
Value of c is 12
Value of a is 9

2) Relational Operators

¾     In C Programming we can compare the value stored between two variables and depending on the result we can follow different blocks using Relational Operator in C.    
¾     Relational operators in c programming is used for specifying the relation between two operands such as greater than, less than and equals.
¾     The following table shows all the relational operators supported by C. Assume variable ‘A’ holds 10 and variable ‘B’ holds 20.
Operator
Description
Example
==
Checks if the values of two operands are equal or not. If yes, then the condition becomes true.
(A == B) is not true.
!=
Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true.
(A != B) is true.
Checks if the value of left operand is greater than the value of the right operand. If yes, then the condition becomes true.
(A > B) is not true.
Checks if the value of left operand is less than the value of the right operand. If yes, then the condition becomes true.
(A < B) is true.
>=
Checks if the value of left operand is greater than or equal to the value of the right operand. If yes, then the condition becomes true.
(A >= B) is not true.
<=
Checks if the value of left operand is less than or equal to the value of the right operand. If yes, then the condition becomes true.
(A <= B)

Program: WAP to demonstrate Relational Operator.

#include<stdio.h>
#include<conio.h>
void main()
   int a=21,b=10,c;
   if(a==b) {
       printf(“Line 1 – a is equal to b\n”);
    }
   else {
       printf(“Line 1 – a is not equal to b\n”);
   }
   if(a < b) {
       printf(“Line 2 – a is less than b\n”);
    }
   else {
       printf(“Line 2 – a is not less than b\n”);
   }
   if(a > b) {
       printf(“Line 3 – a is greater than b\n”);
    }
   else {
       printf(“Line 3 – a is not greater than b\n”);
   }
   if(a <= b) {
       printf(“Line 4 – a is less than or equal to b\n”);
    }
   else {
       printf(“Line 4 – a is not less than or equal to b\n”);
   }
   if(a >= b) {
      printf(“Line 5 – a is greater than or equal to b\n”);
    }
   else {
       printf(“Line 5 – a is not greater than or equal to b\n”);
   }
   getch();
}

Output
Line 1 - a is not equal to b
Line 2 - a is not less than b
Line 3 - a is greater than b
Line 4 - a is not less than or equal to b
Line 5 - a is greater than or equal to b

  3) Logical Operators

            Following table shows all the logical operators supported by C language. Assume variable A holds 1 and variable B holds 0.

Operator
Description
Example
&&
Called Logical AND operator. If both the operands are non-zero, then the condition becomes true.
(A && B) is false.
||
Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true.
(A || B) is true.
!
Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.
!(A && B) 

Program: WAP to demonstrate Logical Operator.


#include<stdio.h>
#include<conio.h>
void main()
   int a=5,b=21,c;
   clrscr();
   if ( a && b ) {
      printf("Line 1 - Condition is true\n" );
   }
   if ( a || b ) {
      printf("Line 2 - Condition is true\n" );
   }
   /* lets change the value of  a and b */
   a = 0;
   b = 10;
   if ( a && b ) {
      printf("Line 3 - Condition is true\n" );
   }
   else {
      printf("Line 3 - Condition is not true\n" );
   }      
   if ( !(a && b) ) {
      printf("Line 4 - Condition is true\n" );
   }
    getch();
}

Output :
Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true

4) Bitwise Operators


P
Q
p & q
p | q
p ^ q
0
0
0
0
0
0
1
0
1
1
1
1
1
1
0
1
0
0
1
1







Bitwise operator works on bits and performs bit-by-bit operation. The truth tables for &, |, and ^ is as follows
Assume A = 60 and B = 13 in binary format, they will be as follows −
A = 0011 1100                            B = 0000 1101
A&B = 0000 1100                      A|B = 0011 1101
A^B = 0011 0001                       ~A = 1100 0011
            The following table lists the bitwise operators supported by C. Assume variable 'A' holds 60 and variable 'B' holds 13, then

Operator
Description
Example
&
Binary AND Operator copies a bit to the result if it exists in both operands.
(A & B) = 12, i.e., 0000 1100
|
Binary OR Operator copies a bit if it exists in either operand.
(A | B) = 61, i.e., 0011 1101
^
Binary XOR Operator copies the bit if it is set in one operand but not both.
(A ^ B) = 49, i.e., 0011 0001
~
Binary Ones Complement Operator is unary and has the effect of 'flipping' bits.
(~A ) = -61, i.e,. 1100 0011 in 2's complement form.
<< 
Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.
A << 2 = 240 i.e., 1111 0000
>> 
Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.
A >> 2 = 15 i.e., 0000 1111



















Program: WAP to demonstrate Bitwise Operator.

#include<stdio.h>
#include<conio.h>
void main()
   unsigned int a = 60;  /* 60 = 0011 1100 */ 
   unsigned int b = 13;  /* 13 = 0000 1101 */
   int c = 0;   
   clrscr();      
   c = a & b;       /* 12 = 0000 1100 */
   printf("Line 1 - Value of c is %d\n", c );
   c = a | b;       /* 61 = 0011 1101 */
   printf("Line 2 - Value of c is %d\n", c );
   c = a ^ b;       /* 49 = 0011 0001 */
   printf("Line 3 - Value of c is %d\n", c );
   c = ~a;          /*-61 = 1100 0011 */
   printf("Line 4 - Value of c is %d\n", c );
   c = a << 2;     /* 240 = 1111 0000 */
   printf("Line 5 - Value of c is %d\n", c );
   c = a >> 2;     /* 15 = 0000 1111 */
   printf("Line 6 - Value of c is %d\n", c );
    getch();
}

Output
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15

 5) Ternary or Conditional Operators

            Conditional operators return one value if the condition is true and returns another value is condition is false.
Syntax     :        (Condition? true_value : false_value);
Example  :         (A > 100  ?  0  :  1);
            In the above example, if A is greater than 100, 0 is returned else 1 is returned. This is equal to if else conditional statements.

Program: WAP to demonstrate Ternary or Conditional Operator.

#include<stdio.h>
#include<conio.h>
void main()
     int x=1, y ;
     clrscr();
     y = ( (x ==1) ? 2 : 0 ) ;
     printf(“x value is %d\n”, x);
     printf(“y value is %d”, y);
    getch();
}
Output
x value is 1
y value is 2

6) Assignment Operators

            In C programs, values for the variables are assigned using assignment operators. For example, if the value “10″ is to be assigned for the variable “sum”, it can be assigned as “sum = 10;” The following table lists the assignment operators supported by the C language.

Operator
Description
Example
=
Simple assignment operator. Assigns values from right side operands to left side operand
C = A + B will assign the value of A + B to C
+=
Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand.
C += A is equivalent to C = C + A
-=
Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand.
C -= A is equivalent to C = C - A
*=
Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand.
C *= A is equivalent to C = C * A
/=
Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand.
C /= A is equivalent to C = C / A
%=
Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand.
C %= A is equivalent to C = C % A
<<=
Left shift AND assignment operator.
C <<= 2 is same as C = C << 2
>>=
Right shift AND assignment operator.
C >>= 2 is same as C = C >> 2
&=
Bitwise AND assignment operator.
C &= 2 is same as C = C & 2
^=
Bitwise exclusive OR and assignment operator.
C ^= 2 is same as C = C ^ 2
|=
Bitwise inclusive OR and assignment operator.
C |= 2 is same as C = C | 2

Program: WAP to demonstrate Assignment Operator.

#include<stdio.h>
#include<conio.h>
void main()
   int a = 21,c ;
   c =  a;
   printf("Line 1 - =  Operator Example, Value of c = %d\n", c );
   c +=  a;
   printf("Line 2 - += Operator Example, Value of c = %d\n", c );
   c -=  a;
   printf("Line 3 - -= Operator Example, Value of c = %d\n", c );
   c *=  a;
   printf("Line 4 - *= Operator Example, Value of c = %d\n", c );
   c /=  a;
   printf("Line 5 - /= Operator Example, Value of c = %d\n", c );
   c  = 200;
   c %=  a;
   printf("Line 6 - %= Operator Example, Value of c = %d\n", c );
   c <<=  2;
   printf("Line 7 - <<= Operator Example, Value of c = %d\n", c );
  c >>=  2;
 printf("Line 8 - >>= Operator Example, Value of c = %d\n", c ); 
  c &=  2;
  printf("Line 9 - &= Operator Example, Value of c = %d\n", c );  
   c ^=  2;
   printf("Line 10 - ^= Operator Example, Value of c = %d\n", c );
   c |=  2;
   printf("Line 11 - |= Operator Example, Value of c = %d\n", c );
   getch();
}

Output
Line 1 -  = Operator Example, Value of c = 21
Line 2 - += Operator Example, Value of c = 42
Line 3 - -= Operator Example, Value of c = 21
Line 4 - *= Operator Example, Value of c = 441
Line 5 - /= Operator Example, Value of c = 21
Line 6 - %= Operator Example, Value of c = 11
Line 7 - <<= Operator Example, Value of c = 44
Line 8 - >>= Operator Example, Value of c = 11
Line 9 - &= Operator Example, Value of c = 2
Line 10 - ^= Operator Example, Value of c = 0
Line 11 - |= Operator Example, Value of c = 2

 7) Selection Operator

            Theses operators are used to select a certain element of a set of elements. The different operators in this set are listed below:

Operator
Description
Example
[]
This operator is used to select an element of an Array
int a[20];

.
This is called as period operator and is used to select an element of a structure or union.
structurename.struct_variable
->
This is used to select an element of a structure or union pointed by a pointer.
This will be studied later
()
This is called as a function call operator and used to call or select a function.
void main(){
}
,
The comma(,) operator is used to separate the different values etc.
int a,b,c;

















¾     Braces{}These opening and ending curly braces marks the start and end of a block of code containing more than one executable statement.
¾     Parentheses(): These special symbols are used to indicate function calls and function parameters.
¾     Brackets[]: Opening and closing brackets are used as array element reference. These indicate single and multidimensional subscripts. 

8) Misc Operator

Besides the operators discussed above, there are a few other important operators including sizeof, & and * supported by the C Language.

Operator
Description
Example
sizeof()
Returns the size of a variable.
sizeof(a), where a is integer, will return 2 byte.
&
Returns the address of a variable.
&a; returns the actual address of the variable.
*
Pointer to a variable.
*a;



Program 1.20: WAP to demonstrate Misc. Operator.

#include<stdio.h>
#include<conio.h>
void main()
{
   int a = 4;
   short b;
   double c;
   int* ptr;
   /* example of sizeof operator */
   printf("Line 1 - Size of variable a = %d\n", sizeof(a) );
   printf("Line 2 - Size of variable b = %d\n", sizeof(b) );
   printf("Line 3 - Size of variable c= %d\n", sizeof(c) );
   /* example of & and * operators */
   ptr = &a;       /* 'ptr' now contains the address of 'a'*/
   printf("value of a is  %d\n", a);
   printf("*ptr is %d.\n", *ptr);
   getch();
}

Output
Line 1 - Size of variable a = 2
Line 2 - Size of variable b = 2
Line 3 - Size of variable c= 8
value of a is  4
*ptr is 4.

No comments: