Operators
Operators
Operators
Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction,
multiplication, division etc on numerical values (constants and variables).
The arithmetic operators are of two major types:
Binary Operators – It works using two of the operators, such as -, +, /, *.
Unary Operators In C – It works by making use of just a single operand (value), such as — and +
+.
Also, Explore Ternary Operator in C.
Relational Operators
A relational operator checks the relationship between two operands. If the relation is true, it returns 1;
if the relation is false, it returns value 0.
Relational operators are used in decision making and loops.
Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common assignment
operator is =
Operator Example Same as
= a+b a=b
+= a+=b a=a+b
- = a - =b a=a-b
*= a*=b a=a*b
/= a/=b a=a/b
%= A%=b a=a%b
Bitwise Operators
During computation, mathematical operations like: addition, subtraction, multiplication, division, etc
are converted to bit-level which makes processing faster and saves power.
Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and ^ is
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 A = 60 and B = 13 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
Bitwise operators are used in C programming to perform bit-level operations.
Operator Meaning
s
& Bitwise and
Operator copies a bit to the result if it exists in both operands.
| Bitwise OR
Operator copies a bit if it exists in either operand.
^ Bitwise exclusive OR
Operator copies the bit if it is set in one operand but not both.
- Bitwise complement
Operator is unary and has the effect of 'flipping' bits
<< Shift left
Operator is unary and has the effect of 'flipping' bits
>> Shift right The left operands value is moved right by the number of bits
specified by the right operand
Left shift operator
The left shift operator is basically a bitwise operator used in C that operates on the bits. This operator
is binary in nature- which means that it needs two of the operands for working. We represent it by the
<< sign.
In case the value of the first available operand is negative, the result generated from the left
shift operation will turn out to be undefined.
In a very similar case, when the value of the second operand is negative, then also we get an
undefined result.
Added to this, we also get undefined results when the value of the second operand is equal to
or greater than the total number of bits present in the first operand.
In short, the left shift operator would only work when both the operands available with us are
positive. But, in case the second operand’s value is zero (0), then this operand won’t be able to
function. It means that the result of any pair, say 30 << 0 is going to be 30 itself.
Unary Operators
Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These
two operators are unary operators, meaning they only operate on a single operand.
Prefix and Postfix Increment/ Decrement Operators
The decrement operators and increment operators are of two major types:
Type of Operator Sample Operator Description/ Explanation
Expression
Postfix Increment p++ The program uses the current value of p and
Operator increases the value of p by 1.
Misc Operators
Operators Description
Sizeof() The sizeof is a unary operator
that returns the size of data
(constants, variables, array,
structure, etc).
& It returns the address of a
memory location of a variable.
* Pointer to a variable.
?: conditional operator (?: in
Ternary operator combination) to construct
conditional expressions.
Operators precedence in C
Operator precedence helps us determine which of the operators in an expression must be evaluated
first in case the expression consists of more than a single operator.
Some operators display higher precedence as compared to the others in the program
The order in which the arithmetic operations are executed in an expression is called 'hierarchy of
operations' or 'operator precedence
Types of Operators
Operator Associativity
1. We only use associativity when we have two or more operators that have the same
precedence in an expression.
The point to note here is that associativity is not applicable when we are defining the order of
evaluation of operands with different levels of precedence.
2. All the operators that have a similar level of precedence have the same associativity. It is very
important, or else the compiler won’t be able to decide what order of evaluation must an
expression follow when it has two operators with the same precedence but different
associativity. For example, – and + have the very same associativity.
3. The associativity and precedence of prefix ++ and postfix ++ are very different from each
other. Here, the precedence of prefix ++ is less as compared to the postfix ++. Thus, their
associativity also turns out to be different. Here, the associativity of prefix ++ is from right to left,
while the associativity of postfix ++ is from left to right
4. We must use a comma (,) very carefully, as it has the least level of precedence among all the
other operators present in an expression.
5. We don’t have chaining of comparison in the C programming language. The Python language
treats the expressions such as x > y > z as x > y and y > z. No similar type of chaining occurs in
the C program
Operators Associativit
Operators Associativity
y
. Left to right >= Left to right
-> Left to right <= Left to right
[] Left to right == Left to right
() Left to right != Left to right
~ Right to left ^ Left to right
! Right to left & Left to right
- Right to left || Left to right
+ Right to left | Left to right
-- Right to left ?: Right to left
++ Right to left && Left to right
* Right to left , Right to left
& Right to left = Right to left
(type) Right to left /= Right to left
sizeof Right to left *= Right to left
% Left to right %= Right to left
/ Left to right -= Right to left
* Left to right += Right to left
- Left to right |= Right to left
+ Left to right ^= Right to left
>> Left to right &= Right to left
<< Left to right >>= Right to left
> Left to right << Right to left
< Left to right