Operators in C

In this article we are going to learn different operators in C, so lets start.

An operator is a symbol that works on the variable or on the values of the variable, which directs the compiler what is to be done. These are used in specific mathematical and logical operations.

There are different types of operators in C, they are:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operator
  • Assignment Operator 
  • Misc. Operators

Let us discuss them one by one.

Arithmetic Operators:

Such kind of operators performs all kind of mathematical operations, they are:

OperatorsNameMeaning
+AdditionAdds two operands
SubtractionSubtracts second operand from the first
*Multiplication Multiplies both the operands
/Division Divides numerator by de-numerator 
%ModuloDivides and returns the remainder value
Arithmetic Operators

Example illustrating the arithmetic operators:

#include<stdio.h>
int main()
{
	int a=20,b=10;
	int res=a+b;//addition operator
	printf("The addition operator=%d",res);
	int res1=a-b;//subtraction operator
	printf("\nThe subtraction operator=%d",res1);
	int res2=a*b;//multiplication operator
	printf("\nThe multiplication operator=%d",res2);
	int res3=a/b;//division operator
	printf("\nThe division operator=%d",res3);
	int res4=a%b;//modulo operator
	printf("\nThe modulo operator=%d",res4);
	return 0;
}

Output:

Mns1nc0saAg3QLR0eAsPzaO1jE8 RLl0OH0aadWSrBVi7zvqz3tXYm0 f0hYL9M9mcqdYgOJkZR

Relational Operator:

As the name suggests a relational operator shows the relation between two operands. The following table shows all the relational operator.

OperatorNameMeaning
==Equal toChecks if the values of the two operands are equal or not,  true when equal.
!=Not equal toChecks if the values of the two operands are equal or not, true when not equal.
>Greater thanChecks which is greater among two operands.
<Less thanChecks which is smaller among two operands.
>=Greater than equals toChecks which is greater or equal to among two operands.
<=Less than equals toChecks which is smaller or equal to among two operands.
Relational Operators

An example illustrating the relational operator:

#include <stdio.h>
int main()
{
    int a = 10, b = 20, c = 10;
    printf("%d == %d is %d \n", a, b, a == b);
    printf("%d == %d is %d \n", a, c, a == c);
    printf("%d > %d is %d \n", a, b, a > b);
    printf("%d > %d is %d \n", a, c, a > c);
    printf("%d < %d is %d \n", a, b, a < b);
    printf("%d < %d is %d \n", a, c, a < c);
    printf("%d != %d is %d \n", a, b, a != b);
    printf("%d != %d is %d \n", a, c, a != c);
    printf("%d >= %d is %d \n", a, b, a >= b);
    printf("%d >= %d is %d \n", a, c, a >= c);
    printf("%d <= %d is %d \n", a, b, a <= b);
    printf("%d <= %d is %d \n", a, c, a <= c);
    return 0;
}

Output:

8QCOt3kvSyawXxJe7OW2snHQAB

Logical Operator:

The operators used to evaluate two or more expressions are called logical operators. The following table shows the operators:

OperatorsNameMeaning
&&Logical andTrue if both the operands are non-zero
||Logical orTrue if any one of the operand is non-zero
!Logical notReverse the logical state of its operand
Logical Operators Table

An example illustrating the logical operator:

#include <stdio.h>
int main()
{
    int a=10, b=20, c=10, res;

    res=(a==b) && (c>b);
    printf("(a == b) && (c > b) is %d \n", res);

    res=(a==b) && (c<b);
    printf("(a == b) && (c < b) is %d \n", res);

    res=(a==b) || (c<b);
    printf("(a == b) || (c < b) is %d \n", res);

    res=(a!=b) || (c<b);
    printf("(a != b) || (c < b) is %d \n", res);

    res=!(a!=b);
    printf("!(a != b) is %d \n", res);

    res=!(a==b);
    printf("!(a == b) is %d \n", res);

    return 0;
}

Output:

SBPJW3MdNUC5wAzIwL2p0lMErerPbz8l9GHkxBG0UzdY1 eHKO6oLbuwVXHjQhqVu5Ax7Oy75INdZOGW z h5TVW8YNt4ruDFCQhw0WQBALJD8L6kGAu1YLrJiUJUT064ZIqPg

Bitwise Operator:

This operator works bit by bit and performs bit by bit operation. The following table shows the bitwise operators:

OperatorsName
&Bitwise AND
|Bitwise OR
^Bitwise exclusive OR
~Bitwise Complement 
<<Shift left
>>Shift right
Bitwise Operators

An example illustrating a bitwise operator:

//example of bitwise operator

#include<stdio.h>

int main()
{
    int a=6,b=4;
    printf("(a&b)=>%d\n",a&b);
    printf("(a|b)=>%d\n",a|b);
    printf("~(a)=>%d\n",~a);
    printf("~(b)=>%d\n",~b);
    printf("(a>>2)=>%d\n",a>>2);
    printf("(a<<2)=>%d\n",a<<2);
    printf("(b>>2)=>%d\n",b>>2);
    printf("(b<<2)=>%d\n",b<<2);
    return 0;
}

Output:

#Ouutput
(a&b)=>4
(a|b)=>6
~(a)=>-7
~(b)=>-5
(a>>2)=>1
(a<<2)=>24
(b>>2)=>1
(b<<2)=>16

Assignment Operator:

This operator is used to assign value to a variable. The table shows the assignment operators:

OperatorName
=Assignment operator
+=Add AND assignment operator
-=Subtract AND assignment operator
*=Multiply AND assignment operator
/=Divide AND assignment operator
%=Modulus AND assignment operator
Shorthand Assignment Operator

Example illustrating the assignment operator:

#include <stdio.h>
int main()
{
    int a = 5, c;

    c = a;
    printf("c = %d\n", c);
    c += a; 
    printf("c = %d\n", c);
    c -= a;
    printf("c = %d\n", c);
    c *= a;
    printf("c = %d\n", c);
    c /= a;
    printf("c = %d\n", c);
    c %= a;
    printf("c = %d\n", c);

    return 0;
}

Output:

U1lziqSPiEgeRkUQipvT 6FvWvSMkRyL82G FLQxs

Misc. Operators:

This operator contains the other operators such as the increment, decrement and sizeof operators. The table below shows the operators:

OperatorsName
++Increment operator
Decrement operator
sizeofSizeof operator
Miscellaneous Operators

Example illustrating the misc. operators:

#include <stdio.h>
int main()
{
    int a = 10, a1 = 20;
    float b;
    double c;
    char d;

    printf("++a = %d \n", ++a);
    printf("--b = %d \n", --a1);
    printf("Size of int=%lu bytes\n",sizeof(a));
    printf("Size of float=%lu bytes\n",sizeof(b));
    printf("Size of double=%lu bytes\n",sizeof(c));
    printf("Size of char=%lu byte\n",sizeof(d));


    return 0;
}

Output:

xpK3Q44F9kDLUYtLXW vRsagjHMXQygWyMCMIXJiUefDtF1QL5hqQJ01N4PTvRo1ENH1Q7vONhn44dVsuAGeWiNOMQmLzJo0v3yG1To3wwP BFyuWut 8UEIUI9MteFo5WWZpg

Here we end our discussion on operators in C, hope u like it and have cleared all your doubts related to this topic.

Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about the topic discussed above.

Share Your Love
Default image
Soham Malakar
Hello my name is Soham Malakar am currently pursuing BCA Coding is one of my favorite hobbies. I love to code and an enthusiast to learn different types of programming languages. I love to get intoxicated in the world of technology and video games. A quick learner possessing better results.
Articles: 36

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply