OPERATORS IN C
An operator is a symbol that tells the compiler to perform specific
mathematical or logical functions. C language is rich in built-in operators and
provides the following types of operators –
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Increment/Decrement Operator
Misc Operators
Arithmetic Operators
The following table shows all the arithmetic operators supported by the C
language. Assume variable A holds 10 and variable B holds 20 then −
Show Examples
Operator Description Example
+ Adds two operands. A + B = 30
− Subtracts second operand from the first. A − B = -10
* Multiplies both operands. A * B = 200
/ Divides numerator by de-numerator. B/A=2
% Modulus Operator and remainder of after B % A = 0
an integer division.
Relational Operators
The following table shows all the relational operators supported by C. Assume
variable A holds 10 and variable B holds 20 then −
Operator Description Example
== Checks if the values of two operands are equal (A == B)
or not. If yes, then the condition becomes true. is not
true.
!= Checks if the values of two operands are equal (A != B)
or not. If the values are not equal, then the is true.
condition becomes true.
> Checks if the value of left operand is greater (A > B)
than the value of right operand. If yes, then the is not
condition becomes true. true.
< Checks if the value of left operand is less than (A < B)
the value of right operand. If yes, then the is true.
condition becomes true.
>= Checks if the value of left operand is greater (A >= B)
than or equal to the value of right operand. If is not
yes, then the condition becomes true. true.
<= Checks if the value of left operand is less than (A <= B)
or equal to the value of right operand. If yes, is true.
then the condition becomes true.
Logical Operators
Following table shows all the logical operators supported by C language.
Assume variable A holds 1 and variable B holds 0, then –
Operator Description Example
&& Called Logical AND operator. If both the (A &&
operands are non-zero, then the condition B) is
becomes true. false.
|| Called Logical OR Operator. If any of the two (A || B)
operands is non-zero, then the condition is true.
becomes true.
! Called Logical NOT Operator. It is used to !(A &&
reverse the logical state of its operand. If a B) is
condition is true, then Logical NOT operator true.
will make it false.
Bitwise Operators
The following table lists the bitwise operators supported by C. Assume variable
'A' holds 60 and variable 'B' holds 13, then –
Operator Description Example
& Binary AND Operator copies a bit to the (A & B) =
result if it exists in both operands. 12, i.e.,
0000 1100
| Binary OR Operator copies a bit if it exists (A | B) =
in either operand. 61, i.e.,
0011 1101
^ Binary XOR Operator copies the bit if it is (A ^ B) =
set in one operand but not both. 49, i.e.,
0011 0001
~ (~A ) = -61,
i.e,. 1100
Binary Ones Complement Operator is unary
0011 in 2's
and has the effect of 'flipping' bits.
complement
form.
<< Binary Left Shift Operator. The left A << 2 =
operands value is moved left by the number 240 i.e.,
of bits specified by the right operand. 1111 0000
>> Binary Right Shift Operator. The left
operands value is moved right by the
number of bits specified by the right
operand.
Assignment Operators
The following table lists the assignment operators supported by the C language
Operator Description Example
= Simple assignment operator. C = A + B will assign
Assigns values from right side the value of A + B to
operands to left side operand C
+= Add AND assignment operator. It
adds the right operand to the left C += A is equivalent
operand and assign the result to to C = C + A
the left operand.
-= Subtract AND assignment
operator. It subtracts the right
C -= A is equivalent to
operand from the left operand and
C=C-A
assigns the result to the left
operand.
*= Multiply AND assignment
operator. It multiplies the right
C *= A is equivalent
operand with the left operand and
to C = C * A
assigns the result to the left
operand.
/= Divide AND assignment operator.
It divides the left operand with C /= A is equivalent to
the right operand and assigns the C = C / A
result to the left operand.
%= Modulus AND assignment
operator. It takes modulus using C %= A is equivalent
two operands and assigns the to C = C % A
result to the left operand.
<<= Left shift AND assignment C <<= 2 is same as C
operator. = C << 2
>>= Right shift AND assignment C >>= 2 is same as C
operator. = C >> 2
&= Bitwise AND assignment C &= 2 is same as C =
operator. C&2
^= Bitwise exclusive OR and C ^= 2 is same as C =
assignment operator. C^2
|= Bitwise inclusive OR and C |= 2 is same as C =
assignment operator. C|2
Increment/Decrement Operator
Assume variable A holds 10
Operator Description Example
++ Increment operator increases the A++ = 11
integer value by one.
-- Decrement operator decreases the A-- = 9
integer value by one.
Increment/Decrement can be two types
1. Pre-Increment/Decrement ( ++ i, - - i )
2. Post-Increment/Decrement ( I ++, i - - )
Misc Operators ↦ sizeof & ternary
Besides the operators discussed above, there are a few other important
operators including sizeof and ? : supported by the C Language.
Operator Description Example
sizeof() sizeof(a), where a is
Returns the size of a variable.
integer, will return 4.
& &a; returns the actual
Returns the address of a variable.
address of the variable.
* Pointer to a variable. *a;
?: If Condition is true ?
Conditional Expression. then value X :
otherwise value Y