Operators in C Programming Language

Operators in C Language

An Operator is a symbol which is used to perform operations on operands in any programming language. An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. Any calculation and sentencing in any language is performed by these operators. These are used with operands.

Operand

It is the data on which the operation of operator occurs. Operators are used to manipulate operands. For example x+y, here x and y are operands and + is operator.

Types of Operators in C Language

The five types of operators which are used in C language are shown as below:

1. Arithmetic operators :

It is the type of operators which take numerical values as their operands and perform some operation on them i.e add, subtract, divide, multiply and return a single numeric value as an output. The code as an example is shown below.

#include <stdio.h>
#include <conio.h>
int main()
{
    int num1, num2, total, sum, sub, div, mul;
    printf("Enter two numbers\n");
    scanf("%d""%d", &num1, &num2);
    sum=num1+num2;
    sub=num1-num2;
    div=num1/num2;
    mul=num1*num2;
    printf("The sum of two numbers is %d\n",sum);
    printf("The subtraction of two numbers is %d\n",sub);
    printf("The division of two numbers is %d\n",div);
    printf("The multiplication of two numbers is %d\n",mul);
    return 0;
}

This code will output as:

Enter two numbers
9
3
The sum of two numbers is 12
The subtraction of two numbers is 6
The division of two numbers is 3
The multiplication of two numbers is 27

2. Relational operators

It checks the relationship between two or more operands or it compares two or more operands. It checks whether one of the operands is lesser, greater or equal to the other operand. If the comparison or relationship is true, then it returns 1. If the comparison or relationship is false, then it returns 0. For example the code shown below.

#include<stdio.h>
#include<conio.h>
int main()
{
    int num1=10, num2=6;
    printf("num1==num2 is %d\n", num1==num2);
    printf("num1!=num2 is %d\n", num1!=num2);
    printf("num1>num2 is %d\n", num1>num2);
    printf("num1<num2 is %d\n", num1<num2);
    printf("num1<=num2 is %d\n", num1==num2);
    printf("num1>=num2 is %d\n", num1==num2);
    return 0;
}
It gives output as:
num1==num2 is 0
num1!=num2 is 1
num1>num2 is 1
num1<num2 is 0
num1<=num2 is 0
num1>=num2 is 1

3. Logical operators

In addition to the relational operators discussed, C supports three logical operators that works on logical expressions. These logical operators are && (AND), ||(OR) and !(NOT). Thus, logical operators can be used to form more complex conditions that are either true or false. Following table shows all the logical operators supported by C language. Assume variable A holds 1 and variable B holds 0

OperatorDescriptionExample
&&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) is true.

4. Bitwise operators

C provides six operators for bit manipulation

xyx&yx|yX^y
00000
01011
10011
11110

5. Assignment operators

The most commonly used assignment operator in C is = and it is used as follows. Suppose name=expression, where name generally represents a variable, constant and more complex expression.

It should be noted that assignment operator = is different from the equality operator ==. The assignment operator is used to assign a value to “name” whereas equality operator is used to compare the values of operands.

= operator It is used to assign the value to the left of operator

+= operator add AND operator  It adds right operand to the left operand and and then assign to it.

-= operator  It subtracts right operand from left operand and result assigns to the left operand.

*=operator It multiply left operand with right operand and result assigns to the left operand.

/=operator  It divides left operand with right operand and result assigns to the left operand.

siizeof() operator It gives the size of a variable.

& Gives the address of a variable.

*a Pointer to a variable (*a).