Operators in C

By | August 17, 2020

Operators are the foundation of any programming language. An operator is a symbol or a special character that tells the computer to perform certain mathematical or logical manipulations which is applied to operands to give a result. It can operate on integer and real numbers.

For example, consider the below statement:

c = a + b; 

Here, ‘+’ is the operator known as addition operator and ‘a’ and ‘b’ are operands. The addition operator tells the compiler to add both of the operands ‘a’ and ‘b’.

C language supports a rich set of built-in operators.

Various types of Operators :

In C, operators in Can be categorized in following categories:

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Assignment Operator
  5. Arithmetic assignment Operators
  6. Increment and Decrement Operators
  7. Conditional Operator
  8. Bitwise Operators
  9. Special Operators

These are given as following –

1. Arithmetic operators :
These Operators are used to perform arithmetic/mathematical operations on operands. There are two types of arithmetic operator : Unary operator (e.g., ++, –), and Binary operator (e.g., +, -, *, /).

Operator Description
++ Increment by one (i.e., Increment operator)
– – Decrement by one (i.e., Decrement operator)
Minus (i.e., Unary minus operator)
+ adds two operands
subtract second operands from first
* multiply two operand
/ divide numerator by denominator
% remainder of division (i.e., mod operation)

2. Relational Operators :
These are used for comparison of the values of two operands.

Operator Description
== Check if are equal (i.e., equality operator)
!= Check if are not equal
> Check if greater than
< Check if less than
>= Check if greater than or equal to
<= Check if less than or equal to

3. Logical Operators :
Logical Operators are used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration.

Operator Description
&& Longical AND, (e.g., x && y)
|| Longical OR, (e.g., x || y)
! Longical NOT, (e.g., !x ), i.e., One’s Complement

4. Bitwise Operators :
Bitwise operators perform manipulations of data at bit level. These operators also perform shifting of bits from right to left.

Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise exlusive OR
<< Bitwise left shift
>> Bitwise right shift

Note –
Bitwise operators are not applied to float or double.

5. Arithmetic assignment Operators :
Assignment operators are used to assign value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value.

Operator Description
= Assign value from right to left side operand (i.e., Assignment Operator)
+= Add by it then assign
-= Subtract by it then assign
*= Multiply by it then assign
/= Divide by it then assign
%= Find mod by it then assign

Note –
Equation a += b is same as a = a+b ; for each above case.

6. Special Operators :
These are also important operators in C :

Operator Description
?: Conditional operator (i.e., Ternary operator). Example: Expression1 ? Expression2 : Expression3
sizeof() size of a variable. Example: sizeof(a) ; // if a is interger type then return 4
, Comma operator, i.e. expression separator.
* Pointer operator, Example: *a;
& Address of operator Example: & a;
. Dot operator. Example: List.Next;
-> Member Access operator. Example: Link -> Next;

Note –
Conditional operator is of the form

Expression1 ? Expression2 : Expression3 

.
Expression1 is the condition to be evaluated.

  • If the condition(Expression1) is True then we will execute and return the result of Expression2, Otherwise we will execute and return the result of Expression3.
  • We may replace the use of if..else statements by conditional operators.



Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.