OPERATORS AND EXPRESSIONS

OPERATORS AND EXPRESSIONS
C supports a rich set of operators. An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. Operators are used in programs to manipulate data and variables. They usually form a part of the mathematical of logical expressions.
C operators can be classified into eight categories as shown below:
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators

1.       ARITHMETIC OPERATORS:
C provides all the basic arithmetic operators (+, -, *, /). The unary minus operator, in effect, multiplies its single operand by -1. Therefore, a number preceded by a minus sign changes its sign.
Operator                            Meaning
+                                          Addition
-                                           Subtraction
*                                         Multiplication
/                                           Division
%                                       Modulo division
Interger   division  truncates  any  fractional  part.

#include <stdio.h>

main()
 {

   int a = 21;
   int b = 10;
   int 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 );
}
When you compile and execute the above program, it produces the following result −
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


2.          RELATIONAL OPERATORS:

We often compare two quantities, and depending on their relation, take certain decisions. For example, we may compare the age of two persons, or the  price of two items, and so on. These comparisons can be done with the help of relational operators.

C supports six relational operators as shown below:


Relational Operators
             Operator          Meaning
                  <                                 is less than
                   <=                              is less than or equal to
                   >                                is greater than
                   >=                              is greater than or equal to
                   !=                               is not equal to
                   ==                              is equal to

                           4.5 <= 10     TRUE
                           4.5 < -10     FALSE
                          -35 >= 0     FALSE
                           10 < 7+5   TRUE
                 a+b = = c+d  TRUE (only if the sum of values of a  and b is equal to the sum of values of c and d.)    

#include <stdio.h>

main()
{

   int a = 21;
   int b = 10;
   int 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" );
   }
  
   /* Lets change value of a and b */
   a = 5;
   b = 20;
       
   if ( a <= b )
   {
      printf("Line 4 - a is either less than or equal to  b\n" );
   }
       
   if ( b >= a )
   {
      printf("Line 5 - b is either greater than  or equal to b\n" );
   }
}
When you compile and execute the above program, it produces the following result −
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 either less than or equal to  b
Line 5 - b is either greater than  or equal to b



3.      LOGICAL OPERATORS:

    C has the following three logical   operators.
   1.  && meaning logical AND
   2.   || meaning logical OR
   3.    ! meaning logical NOT


Some examples of the usage of logical expressions are given below:
1. if(age > 55 && salary < 1000)
2. if(number < 0 || number > 100)

#include <stdio.h>

main() {

   int a = 5;
   int b = 20;
   int c ;

   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" );
   }
       
}
When you compile and execute the above program, it produces the following result −
Line 1 - Condition is true
Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true


·         ASSIGNMENT OPERATORS

Assignment operators are used to assign the result of an expression to a
variable. C has a set of shorthand assignment operators as shown below:
v op = exp;
where v is a varible, exp is an expression and op is a shorthand
assignment operator.
It is equivalent to
v = v op (exp);
 
#include <stdio.h>

main() {

   int a = 21;
   int 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 );

}
When you compile and execute the above program, it produces the following result −
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

Shorthand Assignment Operators:

Statement with simple Statement with
assignment operator             shorthand operator
a = a + 1                                     a + = 1
a = a - 1                                      a - = 1
a = a * a                                      a * =a
a = a*(n+1)                                a* = (n+1)
a = a / (n+1)                               a / = (n+1)
a = a % b                                   a % = b

·         INCREMENT AND  DECREMENT  OPERATORS:

C has two very useful operators not generally found in other languages.
These are the increment and decrement operators:
                                                   + + and - -

Increment and decrement
/ *A program showing the different uses of the increment and decrement operators.  */
int main (void)
{ a, b;
   a = 5;/* a is incremented by 1 */
   ++a;
   printf ("After ++a, a is now %d\n", a); a is once more incremented by 1 */
   a++;
   printf ("After a++, a is now %d\n", a);
   b = a++;
   printf ("After b=a++, a is now %d and b is %d\n", a, b); and b gets the incremented a */
   b = ++a;
   printf ("After b=++a, a is now %d and b is %d\n", a, b); decremented by 1 */
   --a;
   printf ("After --a, a is now %d\n", a); a is once more decremented by 1 */
   a--;
   printf ("After a--, a is now %d\n", a); a is decremented but b gets the current a */
   b = a--;
   printf ("After b=a--, a is now %d and b is %d\n", a, b);
   b = --a;
   printf ("After b=++a, a is now %d and b is %d\n", a, b);              
   return (0);
}
When you compile and execute the above program, it produces the following result −
After ++a, a is now 6
After a++, a is now 7
After b=a++, a is now 8 and b is 7
After b=++a, a is now 9 and b is 9
After --a, a is now 8
After a--, a is now 7
After b=a--, a is now 6 and b is 7
After b=++a, a is now 5 and b is 5


·         CONDITIONAL OPERATOR:
A ternary operator pair “?:” is available in C to construct conditional
expressions of the form
                                          exp1 ? exp2 : exp3;
To find out a number which  is greater than other number.
This can be done by using if.. else statements as follows:
                                a = 10;
                                b = 15;
                                if (a > b)
                                x =a;
                                else
                                x = b;
       These same program can be written as shown below:
                                 x = (a >b)? a: b;
#include<stdio.h>
void main()
{
Int a=5,b=5;
(a>b)?a is greater: b is greater;
}


·         BITWISE OPERATOR

C has a distinction of supporting special operators known as bitwise operators for manipulation of data at bit level. These operators are used for testing the bits, or shifting them right or left. Bitwise operators may not be
applied to float or double.

#include <stdio.h>

main() {

   unsigned int a = 60;        /* 60 = 0011 1100 */ 
   unsigned int b = 13;        /* 13 = 0000 1101 */
   int c = 0;          

   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 );
}
When you compile and execute the above program, it produces the following result −
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

·         SPECIAL   OPERATORS

C supports some special operators such as comma operator, sizeof operator, pointer operators (& and *) and member selection operators ( .  And  ->).

The sizeof Operator
The sizeof is a compile time operator and, when used with an operand, it
returns the number of bytes the operand occupies. The operand may be a
variable, a constant or a data type qualifier.
Example:
m = sizeof(sum);

Comments