Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Operators

An operator is simply a symbol that is used to perform operations.


Operators to perform different types of operations in C language.
o Arithmetic Operators
o Relational Operators
o Logical Operators
o Bitwise Operators
o Assignment Operator
o Unary operator
o Ternary or Conditional Operator
o Misc Operator

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.

Operator Meaning of Operator


+ addition or unary plus
- subtraction or unary minus
* multiplication
/ division
% remainder after division (modulo division)

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.

Operator Meaning Example


s
== Equal to 5 == 3 is evaluated to 0
> Greater than 5 > 3 is evaluated to 1
< Less than 5 < 3 is evaluated to 0
!= Not equal to 5 != 3 is evaluated to 1
>= Greater than or equal to 5 >= 3 is evaluated to 1
<= Less than or equal to 5 <= 3 is evaluated to 0
Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether expression
results true or false. Logical operators are commonly used in decision making in C programming.
Operators Meaning Example
&& Logical AND. True only if all If c = 5 and d = 2 then, expression
operands are true (( c ==5) && (d > 5) )equals to 0.
|| Logical OR. True only if either If c = 5 and d = 2 then, expression
one operand is true ( c ==5)|| (d > 5)equals to 1.
! Logical NOT. True only if the If c = 5 then, expression !
operand is 0 ( c ==5) equals to 0.

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.

Use of the Left Shift Operator in C


We use the left shift operator to shift the bits of available values to the left. It does so by adding zeros to
the right side of the value in the empty spaces that get created due to shifting. It shifts the bits available
for the first operand to the left on the basis of the number of positions that the second operand specifies.

Syntax of Left Shift Operator


variable_name<<number_of_positions

Right Shift Operator


The right shift operator is very similar to the left shift operator. The difference is that it is used for
shifting the bits of the values that are available with us to the right. It does so by replacing the first bits
with zeroes (0) instead. The excess bits that get shifted off to the right of the operator get discarded
after this
We represent the right shift operator using the >> sign

Syntax of Right Shift Operator


shifted_value = old_value >> amount;

Undefined Result Of Operator


Here are some of the cases where left shift operation would lead to an undefined result:

 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

Prefix Increment ++p p increases by a value of 1, then the


Operator program uses the value of p.

Postfix Increment p++ The program uses the current value of p and
Operator increases the value of p by 1.

Prefix Decrement -p p decreases by a value of 1, then the


Operator program uses the value of p.

Postfix Decrement p- The program uses the current value of p and


Operator decreases 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

Category Operator Associativity


Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative */% Left to right
Additive +- Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right

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

You might also like