Understanding the Types of Operators in C Programming | CoderGuy

Hello, friends, This is Ujjwal Sinha, welcome to CoderGuy (A free platform to learn different programming languages) After completing this article you will be able to understand the different types of operators in C programming.


What are Operators?

Operators are special characters which instruct that compiler to perform certain mathematical or logical operations on some operands. In a instruction of C program. Operations are specified by operators while operands can be constants, variables or expressions.


Types of Operators in C programming


  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Assignment Operators
  5. Increment and decrement Operators
  6. Conditional Operators
  7. Bitwise Operators
  8. Special Operators

Types of Operators in C Programming
Types of Operators in C Programming

Let's discuss all these different operators one by one 

1. Arithmetic Operators: Arithmetic operators are some of the C Programming Operator, which are used to perform arithmetic operations includes operators like Addition(+), Subtraction(-), Multiplication(*), Division(/) and Modulus(%). All these Arithmetic operators in C are binary operators which means they operate on two operands.

2. Relational Operators: The operators used to compare two values are called Relational operators. An expression having a relational operator is called relational expression. The value of the relational expression is either one or zero. It is one of the given relation is true and zero if the given relation is false. C has six relational operators.


  1. <  Less than
  2.  > Greater than
  3. <= Less than equal to
  4. >= Greater than equal to
  5. == Equal to
  6. != Not equal to
Example 2*6 < 1*7
                  12 < 7
                   12!=0
3. Logical Operators: An expression that combines two or more relational expressions is called logical expression and the operator used to combine these relational expressions is called a logical operator.


Operators
Meaning
&&
AND
||
OR
!
NOT

 Example 3+5>6+1&&7*5<8*10
                 8>7 &&35<80
                 0||1
                  0
Truth table of logical operators

Operand1
Operand 2
Result
AND (&&)                OR (||)                                NOT (!)
1
1
1
1
0
1
0
0
1
-
0
1
0
1
1
0
0
0
0
-



The logical operators && (AND) and || (OR) are used when we want to test more than one conditions and make decisions. Logical operator! (NOT) reverts the value of operands which may be a single value or an expression.

4.Assignment operators: Assignment operators are used to assign the result of an expression to a variable in addition to the commonly used assignment (=) operator there is a set of shorthand assignment operators that consists of assignment and arithmetic operators.

Following are the shorthand assignment operators and their meanings-

SHORT ASSIGNMENT OPERATORS
USE:
MEANING:
+=
x+=y
x=x+y;
-=
m-=5
m=m-5;
*=
n*=(t+1)
n=n*(t+1);
/=
a/=b
a=a/b;
%=
c%=9
c=c%9;



BENEFITS OF THE SHORTHAND ASSIGNMENT OPERATORS.

•What appears on the left-hand side need not be repeated on the right-hand side and therefore it becomes easier to write a statement.
•Using Short-hand assignment operators a statement becomes concise.

5.Increment and decrement operators: C has two useful operators called increment and decrement operators. Both are unary operators and can be used an integer operand only to increase or decrease its value by 1.
Example  int x,y;
                  x=52;
                   y=2;
                    x++;
                     --y;
Increment and decrement operators are of both types I.e. postfix and prefix. They may appear before or after an operand. In an independent expression these operators do the same thing irrespective of their position but they behave in a different manner when they are used as an expression on the right-hand side of an assignment statement.

6. Conditional Operator (?:): C provides a very useful ternary operator called a conditional operator.
Syntax : exp1?exp2;exp3;
Example : x>y?printf("x is greater"): printf("y is greater");
                 x>y? True exp: false exp;
Condition operators requires three operands. The first operand is a condition expression. The second operand is called true part expression and the third operand is called false part expression. If the first expression is found to be true, the second expression becomes the output of the whole expression otherwise the third expression becomes the output of the whole expression.

7. Bitwise Operators: C provides some special operators known as a bitwise operator which is used for manipulation of data at bits level. These operators are used for testing the bits or shifting the bits from right to left and left to right. Bitwise operators can be used with integer operands only.


In C, the following 6 operators are bitwise operators

The & (bitwise AND) in C or C++ takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.

The | (bitwise OR) in C or C++ takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.

The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.

The << (left shift) in C or C++ takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.

The >> (right shift) in C or C++ takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.

The ~ (bitwise NOT) in C or C++ takes one number and inverts all bits of it.

8. Special operator: C provides some special operators like comma(,), sizeof, caste operator, pointer operator (*), parenthesis operator (( )) and array operator ([ ]).
 Precedence and associativity of operators.

In C programming operators are grouped according to their order of evaluation known as precedence. The precedence of the operator determines how an expression involving more than one operator will be evaluated. The operator of high precedence is evaluated first then operators of low precedence are evaluated.

The operators of the same precedence are evaluated either from left to right or from right to left. This is the rule of evaluation is called the association of operators.



Precedence, Rank associability of various operators is given below.


Operators
Description
Rank
Associativity
()
Parentheses

1

L to R
[]
Array Subscription
.
membership
+
Unary Plus




2




R to L
_
Unary Minus
++
increment
--
decrement
~
Bitwise complement
*
Pointer
&
Address
Size
sizeof
(type)
Type casting
!
Logical not
*
Multiplication

3

L to R
/
Division
%
Modulus division
+
Addition
4
L to R
-
Subtraction
<< 
Bitwise left shift
5
L to R
>> 
Bitwise right shift
Less than

6

L to R
<=
Less than equal to
Greater than
>=
Greater than equal to
== , !=
Equality, Not equality
7



L to R
&
Bitwise AND
8
^
Bitwise XOR
9
|
Bitwise OR
10
&&
Logical AND
11
||
Logical OR
12
?:
Conditional operators
13


R to L
=, *=,-=, +=, /=, &=, |=, %=,^=, <<=, >>=
Assignment and Shorthand assignment operators (including bitwise)

14
,
Comma Operator
15
L to R

Hope this helps, Share this with your loved ones 💓 and help it grow. 😍
Thanks for reading

Post a Comment

0 Comments