What are Operators in Programming?

This article delivers insight into operators used in programming. The main discussion in this article is about Operators, their types, and their use. Read this.

Operators operate on data stored in a specified memory location. Each operator in a programming language has a predefined meaning defined by the grammar of the language. In addition, each inbuilt function in the programming language has an inbuilt definition. The function definition is executed as soon as the function is called. In the same way, each operator in the programming language has a defined meaning, and it is executed as soon as the compiler compiles the operator. The operators in any programming language are syntactically different from the functions in the programming language.

Operators are categorised according to their use. For example, operators that perform mathematical operations are kept in the category of Arithmetic Operator, and Operators that derive results using logic are known as Logical Operators. The Operators that derive results using relations are known as relational operators. The operators that find the scope of a variable, class, or function are called scope resolution operators.

Almost all languages have built in-operators, and some of the languages have the technique to provide new meaning to the inbuilt operators. There also exist languages in which new operators can be defined.

Use of Operators

The operator and functions both have inbuilt definitions, but the syntax of both differ. Programming languages that support Operators also define their special meaning. Each operator work on operands defined by the programmer. Since operators are given special meaning in the language’s grammar and need operands to work on, they are treated as functions, as inbuilt and user-defined functions have a name that specifies their meaning, and the function may or may not take arguments to work on. So operators can be regarded as a function that can be called as per the requirement and have restrictions on the number of parameters supplied to the operators.

Thus operators work on the operands. The operator can come before the operand, or the operator may come after the operand, or the operator may come between the two operands when the operator comes before the operand than it is termed as a prefix when the operator comes after the operand, then it is termed as postfix and when the operator comes in between the two operands than it is termed as an infix.

There are two types of operators first, the Binary Operator and second, Unary Operator.

Binary Operator

The binary operator acts on two operands and delivers result. Thus each binary operation requires two operands to work on. All arithmetic operators take two operands and provide results.

Arithmetic Operators

The high-level language supports floating-point or integer numbers. Arithmetic operators work on these floating-point or integer numbers. The set of an arithmetic operator includes – Addition (+), Subtraction(-), Multiplication(*), Division(/)  and  Modulo(%). These Airthemetic operator takes two operands to provide result. There also exists Airthemetc operators that are Unary. The Unary Airthenetic operator includes increment operator (++) and decrement operator (–). The increment operator increases the value by one, and the decrement operator decreases the value by 1.

Following is the example of Arithmetic Operators in the C programming language.

# include <stdio.h>

int main()
{

     int j;

     j = 5;
                printf("\n");
                printf("The j value is : %d", j);
                printf("\n");
     j = j  + 5;
                printf("\n");
                printf("The j value is : %d", j);
                printf("\n");
     j = j - 2;
                printf("\n");
                printf("The j value is : %d", j);
                printf("\n");
     j = j  * 2;
                printf("\n");
                printf("The j value is : %d", j);
                printf("\n");
     j = j / 4;
                printf("\n");
                printf("The j value is : %d", j);
                printf("\n");
     j++;
                printf("\n");
                printf("The j value is : %d", j);
                printf("\n");
     j++;
                printf("\n");
                printf("The j value is : %d", j);
                printf("\n");
     j --;
                printf("\n");
                printf("The j value is : %d", j);
                printf("\n");    
}
Output

The j value is: 5

The j value is: 10

The j value is: 8

The j value is: 16

The j value is: 4

The j value is: 5

The j value is: 6

The j value is: 5
Code Analysis 

The above code illustrates the use of Arithmetic operators in the C programming language. In this programming example four Arithmetic operators are used, namely, Addition(+), Subtraction(-), Multiplication(*), and Division(/). 
In the above code, Addition on integer variables is performed using the following code,

j = j  + 5;

The second Arithmetic operator is Subtraction(-).
The above subtract two inter numbers. One integer number is stored in a variable j, and the second integer number 2 is used as it is using the following code,

                                                                   j = j - 2;


The third Airthemetic operator that is covered in the above example is Multiplication(*).
In the above code, multiplication is performed on two integer numbers. One number is stored in the variable j, and the second integer number is a numeric value. Following is the code to perform Multiplication.

                                                                  j = j * 2;

The fourth Airthemetic operator that is covered in the above example is Division(\).
In the above code, the division is performed on two integer numbers. One number is stored in the variable j, and the second integer number is a numeric value. Following is the code to perform Multiplication.

                                                                  j = j / 2;
              
The above programming example illustrates the use of the Unary increment operator.
The Unary increment operator increases the value by 1. Following is the code that increments the value of the integer value by one.

                                                                  j++

 
The above programming example illustrates the use of the Unary decrement operator.
The Unary decrement operator decreases the value by 1. Following is the code that decrements the integer value by one.

                                                                  j--

Logical Operator

The set of logical operators comprises AND, OR, and NOT.  Different programming languages implement the logical operator differently. Most of the high-level programming language represents AND by &&OR by || and NOT by !.

The logical operator AND is used to check whether the condition is True or False. The condition is True if both the operands are true. The AND is a binary logical operator.

The logical operator OR is used to check whether the condition on which the OR operator is applied is true or false. The condition is true if any one of the operands on which the OR operator is applied is true, the operand may also be a condition.

The logical operator ! is used to get the logical state of the operand on which it is applied. If the condition on which logical NOT operator is applied is true, it will make it false, and if the condition on which the logical NOT operator applied is false, it will make it true.

#include <stdio.h>

void main() 
{

   int a_1 = 51;
   int b_1 = 200;
   int c_1 ;

   if ( a_1 && b_1 ) 
   {
      printf("Test 1 is true \n" );
   }
	
   if ( a_1 || b_1 ) 
   {
      printf("Test 2 is true \n" );
   }
   
   a_1 = 0;
   b_1 = 100;
	
   if ( a_1 && b_1 ) 
   {
      printf("Test 3 is true \n" );
   } 
   else 
   {
      printf("Test 3 is false \n" );
   }
	
   if ( !(a_1 && b_1) ) 
   {
      printf("Test 4 is true \n" );
   }
	
}
Output

Test 1 is true 
Test 2 is true 
Test 3 is false 
Test 4 is true 
Code Analysis

In the above programming example, the use of a logical operator is illustrated. In this code, the logical operator &&, ||, and ! is used.

The logical operator AND is used as &&. The && operator is a binary operator and is used to check conditions. The && operator will return if both operands are non--zero, and if any one of the operands is 0, it will return false. Following is the code to illustrate this - 

                                                         int a_1 = 51;
                                                         int b_1 = 200;

                                                         if ( a_1 && b_1 ) 
                                                         {
                                                               printf("Test 1 is true \n" );
                                                         }

In this code snippet, the two integer variable is declared, namely, a_1, and b_1. In the if condition the && operator is used. The && operator checks the value of its operands. If the operands are non-zero, then the condition returns true; otherwise, it will return false. In the above code snippet, the value of both the operands are non-zero; therefore, the if condition will evaluate to true, and it will execute the true block. 

Next, the logical OR (||) operator is used. The logical || operator is a binary operator, and if any one of the operands is non-zero, the result is true. This is explained in the following code snippet.

                                                           if ( a_1 || b_1 ) 
                                                          {
                                                               printf("Test 2 is true \n" );
                                                          }

If any one of the variable in the above program is set to non-zero, then the condition in the If statement will return false. This is implemented using the following code snippet.

                                                           a_1 = 0;
                                                          b_1 = 100;
	
                                                          if ( a_1 && b_1 ) 
                                                          {
                                                                printf("Test 3 is true \n" );
                                                          } 
                                                          else 
                                                          {
                                                                printf("Test 3 is false \n" );
                                                          }                                                     

In this code snippet, the variable a_1 = 0 and b_1 =100. Since one of the variable values is zero, the condition in the if statement will return false. Then false block of the if statement will be executed.

The next logical operator is NOT (!). The NOT (!) is capable of negating the value, the ! Operator will make true to false and false to true. This is achieved by executing the following programming instructions.

                                                           if ( !(a_1 && b_1) ) 
                                                          {
                                                                printf("Test 4 is true \n" );
                                                          }

In this code snippet, the condition in the If statement is returning false but since the logical operator ! is used, then it will convert false to true.

Relational Operators

Relational operators are used to check the relation that exists between two entities. In programing languages such as C, the relational operator compares two operands and return an integer value if o is returned than it means the condition is false and non-zero value reflects that the condition is true.

Relational operators are used for developing relational expressions. For example, a high-level programming language such as C has six relational operators. This relational operator includes –  equal to, not equal to, greater than, less than, greater than or equal to, and less than or equal to. The table below lists the relational operator that is used in the C programming language.

equal to=
not equal to
greater than>
less than<
greater than or equal to 
less than or equal to 

Equal to Operator (= =)

Equal to operator compares two operands, if both the operands are equal, then it returns 1, and if both the operands are not equal, it returns 0. Syntax of Equal to operator is as follows:

Opr1 == Opr2

Following is the programming example of equal to operator in C programming language:

#include <stdio.h>  
#include <math.h>  
int main ( )  
{  
    int a = 5;  
    int b = 10;  
      
  
    printf ("The result of a == b : %d", (a == b)); 
    printf("\n");
    if (a == b)  
    {
        printf("\n");
        printf (" The Realtional Operator syas -\n %d is equal to %d", a, b);  
        printf("\n");
    }
    else
    {
        printf("\n");
        printf ("The Realtional Operator syas - \n %d is not equal to %d", a, b);  
        printf("\n");
    }
  
      
    int x = 5;  
    int y = 5;  
      
    printf("\n");  
    printf ("The Realtional Operator syas - \n x == y : %d", (x == y));  
    printf("\n");
    
    if (a == b) 
    {
        printf("\n");
        printf ("The Realtional Operator syas - \n %d is equal to %d", x, y);  
        printf("\n");
    }
    else
    {
        printf("\n");
        printf ("The Realtional Operator syas - \n %d is not equal to %d", x, y);  
        printf("\n");
    }
    return 0;     
}    
Output

The result of a == b : 0
The Relational Operator syas - 
 5 is not equal to 10
The Realtional Operator syas - 
 x == y : 1
The Relational Operator syas - 
 5 is not equal to 5
Code Analysis

In the above code, the explanation of the Relational operator is given. This example code gives the use of equal to operator. In this code, the value of the two variables is compared. The values of variables are compared using the relational operator “equal to”. 

In the above code, the comparison is made using the “equal to” operator, first, in the printf( ) function and second, in the if statement. 

When the comparison is made in the printf statement the code is like as follows:

                                   printf ("The result of a == b : %d", (a == b));

When the comparison is made in the if statement, then code is like this:

                     if (a == b)  
                     {
                             printf("\n");
                             printf (" The Realtional Operator syas -\n %d is equal to %d", a, b);  
                             printf("\n");
                      }
                      else
                      {
                             printf("\n");
                            printf ("The Realtional Operator syas - \n %d is not equal to %d", a, b);  
                             printf("\n");
                      }

Not equal to

The Not Equal to Operator checks the condition created using two operands. If both the operands are not equal, it returns 1; otherwise, it returns 0.

Syntax to use Not Equal to operator is:

                                             Opr1 != Opr2 ;

Example program to explain the use of not equal to operator.

int main ()  
{  
    int a = 5;  
    int b = 10;  
      
    
    printf ("  The result a != b : %d", (a != b)); 
    printf("\n");
    if (a != b)
    {
        printf("\n");
        printf ("The result of Not Equal to operator is \n %d is equal to %d", a, b);  
        printf("\n");
    }
    else
    {
        printf("\n");
        printf ("The result of Not Equal to operator is \n %d is not equal to %d", a, b);  
        printf("\n");
    }
  
      
    int x = 5;  
    int y = 5;  
      
    
    printf ("The result of Not Equal to operator is \n x != y : %d", (x != y));  
    printf("\n");
    if (a != b)  
    {
        printf("\n");
        printf ("The result of Not Equal to operator is \n %d is equal to %d", x, y);  
        printf("\n");
    }
    else
    {
        printf("\n");
        printf ("The result of Not Equal to operator is \n %d is not equal to %d", x, y);  
        printf("\n");
    }
    return 0;     
}  
Output

  The result a != b : 1
The result of Not Equal to operator is 
 5 is equal to 10
The result of Not Equal to operator is 
 x != y : 0
The result of Not Equal to operator is 
 5 is equal to 5
Code Analysis

In this example code, the use of “not equal operator” is explained. In this example code, two integer variables, ‘a’ and ‘b’ are declared. The “not equal operator” is used in the printf( ) statement. Using the following example code:

                                      printf ("  The result a != b : %d", (a != b)); 

“not equal operator” is also used in the if statement. The code of this is like this:

if (a != b)
    {
        printf("\n");
        printf ("The result of Not Equal to operator is \n %d is equal to %d", a, b);  
        printf("\n");
    }
    else
    {
        printf("\n");
        printf ("The result of Not Equal to operator is \n %d is not equal to %d", a, b);  
        printf("\n");
    }