Please enable Javascript to correctly display the contents on Dot Net Tricks!

Operators

Posted On : 13 Dec 2015
Updated On : 22 Dec 2015
Total Views : 776

C language supports a rich set of built-in operators. An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. Operators in programming languages are taken from mathematics.

Types of Operators

  1. Arithmetic operators

  2. Relational operators

  3. Logical operators

  4. Bitwise operators

  5. Assignment operators

  6. Type Information Operators(Special operators)

operators in c

Arithmetic operators

An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. Operators in programming languages are taken from mathematics.

Operators
Name
C Example
+
Addition
X + Y will give 7
-
Substraction
X - Y will give 3
*
Multimlication
X * Y will give 10
/
Divition
X / Y will give 2
%
Modulus
X % Y will give 1
++
Preincrement and Postincrement
X = ++Y, X = Y++ will give 3, 2
--
Predecrement and Postdecrement
X = --Y, X = Y-- will give 4, 2

Relational operators

These operators are used to compare values and always result in boolean value (True or False). The following is a table of relational operators in C. Suppose you have two integer variables X, Y and having values 5, 2 respectively then

Operators
Name
C Example
>
Greater than
X > Y will give True
<
Less than
X < Y will give False
>=
Greaterthan or Equalto
X >= Y will give True
<=
Lessthan or Equalto
X <= Y will give False
>==
Equalto
X == Y will give False
!=
NotEqualto
X != Y will give True

Logical operators

These operators are used to compare values and always result in boolean value (True or False). The following is a table of logical operators in C. Suppose you have two boolean variables X, Y and having values True, False respectively then

These operators are used to perform logical operations on the given two variables.

Operators
Name
C Example
&&
Logical AND
X && Y will give False
||
Logical OR
X || Y will give True
>!
Logical NOT
!(X), !(Y) will give False, True

Bitwise operators

These operators work on bits of a binary number and perform bit by bit operation. The following is a table of bitwise operators in C. Suppose you have two integer variables X, Y and having values 4, 5 respectively and theirs binary equivalent would be 100, 101 then

Operators
Name
C Example
|
Bitwise OR
X | Y will give 5 i.e. 101
&
Bitwise AND
X & Y will give 4 i.e. 100
~
Bitwise NOT
~(X), ~(Y) will give -5, -6 i.e. -101, -110
^
Bitwise Exclusive OR
X^Y will give 1 i.e. 1

Assignment operators

These operators are used to assign a new value to a variable, a property, an event, or an indexer element. The following is a table of assignments operators in C. Suppose you have two integer variables X, Y and having values 5, 2 then

Operators
Name
C Example
=
Equalto
X = Y will give 2
+=
Plus Equalto
X += Y will give 7
-=
Minus Equalto
X -= Y will give 3
*=
Multiply Equalto
X *= Y will give 2
/=
Divide Equato
X /= Y will give 2
%=
modulus Equalto
X %= Y will give 1

Type Information Operators (Special operators)

These operators are used to provides information about a particular type. The following is a table of type information operators in C.

Operators
Description
Example
sizeof()
Returns the size of a value type.
sizeof(int) will give 2

Misc. Operators

There are some more important operators supported by C. The following is a table of some Misc operators in C. Suppose you have two integer variables X, Y and having values 5, 2 then

Operators
Name
C Example
?:
Conditional Expression/Ternary Operator
X > Y ? X : Y will give 5
*
Pointer to a variable
*X will pointer to X variable.
&
Returns memory address of a variable
X > &X will give address of XY ? X : Y will give 5