C Operators
7. C Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. 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
Misc Operators
This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other
operators one by one.
Arithmetic Operators
Following table shows all the arithmetic operators supported by C language. Assume
variable A holds 10 and variable B holds 20, then:
Operator Description Example
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiplies both operands A * B will give 200
/ Divides numerator by de-numerator B / A will give 2
% Modulus Operator and remainder of after an integer division B % A will give 0
++ Increments operator increases integer value by one A++ will give 11
-- Decrements operator decreases integer value by one A-- will give 9
Try the following example to understand all the arithmetic operators available in C
programming language:
#include<stdio.h>
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1
Line 6 - Value of c is 21
Line 7 - Value of c is 22
Relational Operators
Following table shows all the relational operators supported by C language. Assume
variable A holds 10 and variable B holds 20, then:
Operator Description Example
== Checks if the values of two operands are equal or not, if
yes then condition becomes true. (A==B) is not true.
!= Checks if the values of two operands are equal or not, if
values are not equal then condition becomes true. (A != B) is true.
> Checks if the value of left operand is greater than the
value of right
operand, if yes then condition becomes
true. (A > B) is not true.
< Checks if the value of left operand is less than the value
of right
operand, if yes then condition becomes true. (A < B) is true.
>= Checks if the value of left operand is greater than or
equal
to the value of right operand, if yes then condition
becomes true. (A >= B) is not true.
<= Checks if the value of left operand is less than or equal
to the
value of right operand, if yes then condition
becomes true. (A <= B) is true.
Try the following example to understand all the relational operators available in C
programming language:
#include<stdio.h>
printf("Line 4 - a is either less than or equal to b\n" );}
if ( b >= a ){
printf("Line 5 - b is either greater than or equal to b\n" );}
}
When you compile and execute the above program, it produces the following result:
Line 1 - a is not equal to b
Line 2 - a is not less than b
Line 3 - a is greater than b
Line 4 - a is either less than or equal to b
Line 5 - b is either greater than or equal to b
Bitwise Operators
Bitwise operator works on bits and performs bit-by-bit operation. The truth tables for &, |,
and ^ are as follows:
P q p & q p | q p ^ q
0 0 0 0 0 0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Assume if A = 60; and B = 13; now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
Assignment Operators
There are following assignment operators supported by C language:
= Simple assignment operator, Assigns C = A + B will assign value
values
from right side operands of A +
B into C
to left side operand
+= Add AND assignment operator, It adds C += A is equivalent to C = C + A
right
operand to the left operand and
assign the result
to left operand
-=
Subtract AND assignment operator, C -= A is equivalent to C = C - A
It subtracts
right operand from the left
operand and assign
the result to left
operand
*=
Multiply AND assignment operator, C *= A is equivalent to C = C * A
It multiplies
right operand with the left
operand and assign
the result to left
operand
/=
Divide AND assignment operator, C /= A is equivalent to C = C / A
It divides left
operand with the right
operand and assign the
result to left
operand
%=
Modulus AND assignment operator, C %= A is equivalent to C = C % A
It takes
modulus using two operands
and assign the
result to left operand
<<= Left shift AND assignment operator C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator C &= 2 is same as C = C & 2
^= bitwise exclusive OR and assignment C ^= 2 is same as C = C ^ 2
operator |= bitwise inclusive OR and assignment C |= 2 is same as C = C | 2
operator
Misc Operators ↦ sizeof & ternary
There are few other important operators including sizeof and ? : supported by C Language
Operator Description Example
sizeof() Returns the size of an variable. sizeof(a), where a is integer,
will return 4.
& Returns the address of an variable. &a; will give actual address of
the variable.
* Pointer to a variable. *a; will pointer to a variable.
? : Conditional Expression If Condition is true ? Then
value X : Otherwise value Y
Comments