Operators in C programming Language(Updated 2023) – EngineerSoch

C operators are one of the features in C which has symbols that can perform various tasks as per the need of the program. There are many operators like as:

  • Arithmetic Operator
  • Increment/Decrement Operator
  • Assignment Operator
  • Relational Operator
  • Logical Operator
  • Bitwise Operator

Arithmetic Operator

Arithmetic Operators are the operators which are used to perform mathematical calculations like addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). It performs all the operations on numerical values (constants and variables).

The following table below shows all the arithmetic operators supported by the C language for performing arithmetic operations:

OperatorsDescription
+ (Addition)To add two operands
– (Subtraction)To subtract two operands
* (Multiplication)To multiply operands
/ (Division)To divide operands
% (Modulus)To find remainder
Description for arithmetic operators

Let’s look at an example of Arithmetic operations in C

// Arithmetic operators in C example
#include <stdio.h>
int main()
{
    int a = 12,b = 2, c;    
    c = a+b;
    printf("a+b = %d \n",c);
    c = a-b;
    printf("a-b = %d \n",c);
    c = a*b;
    printf("a*b = %d \n",c);
    c = a/b;
    printf("a/b = %d \n",c);
    c = a%b;
    printf("Remainder when a is divided by b = %d \n",c);   
    return 0;

}

Output:

a+b = 14
a-b = 10
a*b = 24
a/b = 6
a%b = 0

Increment/Decrement Operator

C programming has basically two operators which can increment (++) and decrement (–) the value. It can change the value of an operand by 1. Increment and Decrement Operators are very useful operators that are generally used to minimize the calculation. These two operators are unary operators, which means they can only operate on a single operand. For example, ++x and x++ means x=x+1 or –x and x−− means x=x-1. 

There is a distinction between ++ or −− when written before or after any operand. 

If we use the operator as a prefix, it adds 1 to the operand, and the result is assigned to the variable on the left. Whereas, when it is used as a post-fix, it first assigns the value to the variable on the left i.e., it first returns the original value, and then the operand is incremented by 1.

Operator Description
++It increases the integer value by 1.
– –It increases the integer value by 1.
Description of Increment / Decrement operators
//Example of increment and decrement operators
#include <stdio.h>
int main()
{
    int p = 14, q = 70;
    float r = 120.6, s = 34.5;
    printf("++p = %d \n", ++p);
    printf("--q = %d \n", --q);
    printf("++r = %f \n", ++r);
    printf("--s = %f \n", --s);
    return 0;

}

Output:

++p = 15
--q = 69
++r = 121.600000
--d = 33.500000

Assignment Operator

An assignment operator is mainly responsible for assigning a value to a variable in a program. Assignment operators are used to assign the result of an expression to a variable. This operator assigning the values to any variable. The most common assignment operator is ‘=’. 

C language has a collection of shorthand assignment operators that can be used for C programming.

There is a list of all assignment operators :

OperatorDescriptionExample
=Assign
It used to assign the values from right side of the operands to left side of the operand.
Z = X+Y will assign the value of X+Y to Z
+=Add then assign
It adds the value of the right operand to the value of the left operand and assigns the result to the left operand.
Y += X is same as Y = Y+X
-=Subtract then assign
It subtracts the value of the right operand from the value of the left operand and assigns the result to the left operand.
Y -= X is same as Y = Y – X
*=Multiply then assign
It multiplies the value of the right operand with the value of the left operand and assigns the result to the left operand.
Y *= X is same as Y = Y * X
/=Divide then assign
It divides the value of the left operand with the value of the right operand and assigns the result to the left operand.
Y /= X is same as Y = Y / X
%=Modulus then assign
It takes modulus using the values of the two operands and assigns the result to the left operand.
Y %= X is same as Y = Y % X
<<=Left shift and assign
Used for left shift AND assignment operator.
Z <<= 4 is same as Z = Z << 4
>>=Right shift and assign
Used for right shift AND assignment operator.
Z >>= 5 is same as Z = Z >> 5
&=Bitwise AND assign
Used for bitwise AND assignment operator.
Z &= 7 is same as Z = Z & 7
^=Used for bitwise exclusive OR and assignment operator.Z ^= 6 is same as Z = Z ^ 6
|=Used for bitwise inclusive OR and assignment operator.Z |= 9 is same as Z = Z | 9
Description for assignment operators
//Example of assignment operators
#include <stdio.h>
int main()
{
    int a = 7, b;
    b = a;      // b is 7
    printf("b = %d\n", b);
    b += a;     // b is 14 
    printf("b = %d\n", b);
    b -= a;     // b is 7
    printf("b = %d\n", b);
    b *= a;     // b is 49
    printf("b = %d\n", b);
    b /= a;     // b is 7
    printf("c = %d\n", c);
    b %= a;     // b = 0
    printf("b = %d\n", b);

    return 0;
}

Output:

b = 7
b = 14 
b = 7
b = 49 
b = 7
b = 0

Relational Operator

Relational operators are specifically used to compare two quantities or values in a program. It checks the relationship between two operands. If the given relation is true, it will return 1 and if the relation is false, then it will return 0. Relational operators are heavily used in decision-making and performing loop operations.

The Table below shows all the relational operators:

OperatorDescription
==It is used if the values of the two operands are equal or not. If the operands are equal, then condition becomes true.
!=It is used to check if the values of the two operands are equal or not. If the values are not equal, then the condition becomes true.
>It is used to check if the value of left operand is greater than the value of right operand. If the left operand is greater, then the condition becomes true.
<It is used to check if the value of left operand is less than the value of right operand. If the left operand is lesser, then the condition becomes true.
>=It is used to check if the value of left operand is greater than or equal to the value of right operand. If the value of the left operand is greater than or equal to the value, then the condition becomes true.
<=It is used to check if the value of left operand is less than or equal to the value of right operand. If the value of the left operand is less than or equal to the value, then the condition becomes true.
Description for Relational operators
// Example of relational operators
#include <stdio.h>
int main(
{
    int x = ,9 y = 11;
   printf("%d == %d is False(%d) \n", x, y, x == y);
   printf("%d != %d is True(%d) \n ", x, y, x != y);
   printf("%d > %d is False(%d)\n ",  x, y, x > y);
   printf("%d < %d is True (%d) \n", x, y, x < y);
   printf("%d >= %d is False(%d) \n", x, y, x >= y);
   printf("%d <= %d is True(%d) \n", x, y, x <= y);

    return 0;
}

Output:

9 == 11 is False(0)
9 != 11 is True(1)
9 > 11 is False(0)
9 < 11 is True(1)
9 >= 11 is False(0)
9 <=11 is True(1) 

Logical Operator

In C programming language, we have three logical operators when we have to more than one condition to make decisions then we use logical operators. Logical operators are: 

  • && (AND)
  • || (OR)
  • ! (NOT)

In C language an expression containing a logical operator returns only 0 or 1 depending upon the condition whether the expression results in true or false. It is also known as decision- making operator.

Table of logical operators:

We are here assuming that the variable A holds 8 and variable B holds 2.

OperatorDescriptionExample
&&This is the AND operator in C programming language. It performs logical conjunction of two expressions.((A==8)&&(B>6)) equals to 0
| |It is the NOT operator in C programming language. It performs a logical disjunction on two expressions.((A==8) || (B>7)) equals to 1
!It is the Logical NOT Operator in C programming language. It is used to reverse the logical state of its operand. If a condition is true, then the Logical NOT operator will make it false and vice versa.!(A && B) is true
Description for logical operators

Example of logical operators :-

// Example of logical operators

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

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

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

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

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

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

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

    return 0;
}

Output:

(a == b) && (c > b) is 1 
(a == b) && (c < b) is 0 
(a == b) || (c < b) is 1 
(a != b) || (c < b) is 0 
!(a != b) is 1 
!(a == b) is 0 

Bitwise Operator

Bitwise operators are the operators which work on bits and perform the bit-by-bit operation. Mathematical operations like addition, subtraction, multiplication and division are converted to bit-level which makes processing faster and easier to implement during computation and compiling of the program.

The truth table for &(AND), | (OR) and ^(XOR) is given below:

AB&(AND)|(OR)^(XOR)
00000
01011
10011
11110
Truth Table for Bitwise operators

Here, we will assume that A = 51 and B = 26 in binary format as follows.

A = 00110011

B = 00011010

A&B = 00010010

A|B  = 00111011

A^B = 00101001

~A  = 11001100

The table provided below demonstrates the bitwise operators :

OperatorsDescriptionExample
&Binary AND Operator
It copies a bit to the result if it exists in both the operands.
(A&B)=18 — 00010010
|Binary OR Operator
It copies a bit if and only if it exists in either operand.
(A|B)=59 — 00111011
^Binary XOR Operator
It copies the bit only if it is set in one operand but not both.
(A^B)=41 — 00101001
~Binary One’s Complement Operator 
It is unary and has the effect of ‘flipping’ bits.
(~A)= 11001100
Description for Bitwise operators

Leave a Reply

Your email address will not be published. Required fields are marked *

Solverwp- WordPress Theme and Plugin