February 25, 2019

Types of Operators in C

Only by using few predefined header files and other functions one can't execute a program. There are certain special characters on keyboard that plays another vital role in a code. Those special keys are operators. In this article we'll get to know, different types of Operators useful for C program and their utilities. Let's find out.

Types of Operators in C
Types of Operators in C

What are Operators in C?

Operators are special characters that are used in a program for manipulating data and variables and create a part of the mathematical/logical expressions.

Types of Operators in C:

C language supports different types of Operators that serve different purposes listed as follows:
  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Assignment Operators
  5. Increment and Decrement Operators
  6. Conditional Operator
  7. Bitwise Operators
  8. Special Operators

Arithmetic Operators:

You're very well acquainted with arithmetic Operators as they are being used in everyday life. Addition (+), Subtraction (-), Product (*), Division (/) and Modulus (%) fall under Arithmetic Operators.

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulus

Relational Operators:

Like the name goes, this class of Operators help us to draw conclusion on relationship between two values/operands. Simply putting it helps to compare two different quantities.

OperatorDescription
==Is equal to
!=Is not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Logical Operators:

In C we get three logical operators to help us testing more than one conditions simultaneously. The logical operators are: && (meaning logical AND), || (meaning logical OR) and ! (meaning logical NOT).


OperatorDescription
&&And operator. It performs logical conjunction of two expressions. (if both expressions evaluate to True, result is True. If either expression evaluates to False, the result is False)
||Or operator. It performs a logical disjunction on two expressions. (if either or both expressions evaluate to True, the result is True)
!Not operator. It performs logical negation on an expression.


Assignment Operators:

Assignment operators are used to set the result of an expression to a variable. This shouldn't sound new to you as we are solving such problems from a junior age. C has a collection of shorthand assignment operators. The shorthand one's should be new to you.

OperatorDescription
=Assign
+=Increments then assign
-=Decrements then assign
*=Multiplies then assign
/=Divides then assign
%=Modulus then assign
<<=Left shift and assign
>>=Right shift and assign
&=Bitwise AND assign
^=Bitwise exclusive OR and assign
|=Bitwise inclusive OR and assign

In the above table, if I write a+=5, is same as a=a+5. Same goes for decrement, product, division and modulus.

Increment and Decrement Operators:

These are very helpful and most frequently used operators. There are again two types of Increment and Decrement Operators namely, Post and Pre, indicating position of operators in the code. For example, if we write a++; it is same as a=a+1. Similarly, a-- means a=a-1.


OperatorDescription
++Increment
−−Decrement

But is a++ and ++a same?
The pre-increment first adds one to the operand and after that the result is assigned to the variable on the left whereas post-increment first assigns the value to the variable on the left and then increases the value of the operand. Identical explanation is valid for Decrement Operators too.

Conditional Operators:

Also referred as ternary operator which is the conditional operator (?: in combination) to construct conditional expressions.

OperatorDescription
? :Conditional Expression

Bitwise Operators:

C provides a special operator for binary digit operation between two variables.

OperatorDescription
<<Binary Left Shift Operator
>>Binary Right Shift Operator
~Binary Ones Complement Operator
&Binary AND Operator
^Binary XOR Operator
|Binary OR Operator


Special Operators:

Other than all the above operators, C also comes bundled with the following operators than serve specific purposes:

OperatorDescription
sizeof()Returns the size of a memory location.
&Returns the address of a memory location.
*Pointer to a variable.

Books I Prefer:



  

I hope this article was helpful to understand different operators in C! Comment below, if you've got any question. Head back soon for another interesting article on C Programming.