Operators In C Programming Part: 1 - Ocean of Programming

Saturday 29 April 2017

Operators In C Programming Part: 1


            An operator is used to tells the compiler to perform specific mathematical or logical manipulation. Operators are used in the program to manipulate data and variables. The operator needs operand. An operand is a data item on which an operator acts. The operand may be one or more than one. At least one operand is required. 
          C has a large number of an operator that can be categorized in following groups.
  1.  Arithmetic Operators 
  2. Relational Operator
  3.  Logical Operators
  4. Comma Operator
  5.  Bitwise Operators
  6. sizeof Operators
  7. Increment and Decrement Operators
  8. Assignment Operators
  9. Conditional Operators
I will post all operators in 2 parts all with one reference program. In part 1 first 4 operators and in part 2 remaining 4 operators. Let us see one by one operator in detail.

1. Arithmetic Operators 

Arithmetic operators are used perform arithmetic operations. They are of two types are as follows
A. Unary  Arithmetic Operator
B. Binary Arithmetic Operator

A. Unary Arithmetic Operator  

Unary operator name suggests itself it require only one operand to operate.
e.g. +y, -x
It changes the sign of the operand

B. Binary Arithmetic Operator

There are 5 types of Binary Arithmetic Operators. It name suggest that it require 2 operands to be operated.
Operator Purpose
+ Addition
- Subtraction
* Multiplication
/ Division
% Gives the remainder after division
          Note that the %(modulus operator) cannot be applied on floating point operands. Let us see one example how these operators are used. This example is already discussed in Basic C Programs Part: 1 let us the same example.
#include<stdio.h>
main()
{
        float a,b;
        printf("Enter value of a and b\n");
        scanf("%f %f",&a,&b);
        printf("Addition is=%f\n",a+b);         //Print result of addition
        printf("Subtraction is=%f\n",a-b);      //Print result of subtraction
        printf("Multiplication is=%f\n",a*b);   //Print result of multiplication
        printf("Division is=%f\n",a/b);         //Print result of division
        return 0;
}
         The above example shows how Binary Arithmetic Operators are used. The output for the above program is

2. Relational Operators

            Relational operators are used to comparing the values of two operators. These operators compare two values and result in one of two possible outcomes either true or false. It can compare character values or numeric values. Relational operators are as below.
Operator Meaning
< Less than
<= Less than or equal to
== Equal to
!= Not equal to
> Greater than
>= Greater than or equal to
          This Relational operator always returns a value 0 or 1 only. Let us consider following program for better understanding.
#include <stdio.h>
main()
{
        int a=5,b=7,c=10,e=5;

        printf("%d<%d=%d,%d<%d=%d\n",a,b,a<b,c,e,c<e);
        printf("%d<=%d=%d,%d<=%d=%d\n",a,b,a<=b,c,e,c<=e);
        printf("%d==%d=%d,%d==%d=%d\n",a,e,a==e,c,b,c==b);
        printf("%d!=%d=%d,%d!=%d=%d\n",a,e,a!=e,c,b,c!=b);
        printf("%d>%d=%d,%d>%d=%d\n",a,b,a>b,c,e,c>e);
        printf("%d>=%d=%d,%d>=%d=%d\n",a,e,a>=e,c,b,c>=b);
        return 0;

}
The output for the above program is.

3. Logical Operators

        Logical operators combine two or more logical expressions and give 0 or 1 depending on the result is true or false. If the result is true it returns 1 and if the result is false it returns 0. There are 3 types of logical operators as
Operator Name
&& AND
|| OR
! Not
The truth table for And, Or and Not operator
Expression 1 Expression 2 && (Result) || (Result)
True(1) True(1) True(1) True(1)
True(1) False(0) False(0) True(1)
False(0) True(0) False(0) True(1)
False(0) False(0) False(0) False(0)
Expression !(Result)
True(1) False(0)
False(0) True(1)
Let us consider the following  C program for better understanding.
#include<stdio.h>
main()
{
        int a=5,b=7,c=10,e=5,result;

        result=(a<b)||(a>e);
        printf("Result for (a<b) || (a>e) is=%d\n",result);
        result=(a>b)||(a<e);
        printf("Result for (a>b) || (a<e) is=%d\n",result);
        result=(a!=5)||(a<c);
        printf("Result for (a!=5) || (a<C) is=%d\n",result);
        result=(a<b)&&(a>e);
        printf("Result for (a<b) && (a>e) is=%d\n",result);
        result=(a>b)&&(a<e);
        printf("Result for (a>b) && (a<e) is=%d\n",result);
        result=!(a==e);
        printf("Result for !(a==e) is=%d\n",result);
        result=!(a!=e);
        printf("Result for !(a!=e); is=%d\n",result);
 return 0;
}
 The output for above program is 

 4. Comma Operator

           The comma operator(,) is used to permit different expressions to appear in situation where only one expression would be used. The expressions are separated by comma operator.  This operator is Binary Operator.  The comma operator has the lowest precedence of any C operator, and acts as a sequence point. The comma operator helps make code more compact. Let us see one program for comma operator.
#include <stdio.h>
main()
{
       int x=8, y=5, z=10, sum;
        sum=(x, y, z, x+y+z);
        printf("Sum=%d\n",sum);
        return 0;
}
The output for above program is.
           Please feel free to comment if you find anything incorrect or you want to share more information about the topic discussed above.

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete