Operators in C
Operators in C
The data items on which the operators are applied are known as operands. Operators are applied between the
operands.
Unary Operators
Binary Operators
o Arithmetic Operators
o Relational Operators
o Logical Operators
o Bitwise Operators
o Assignment Operators
Ternary Operator
It involves the use of 3 operands. For example ?: is used in place of if-else condition.
Binary Operators
1. Arithmetic Operators
Following table shows all the arithmetic operators supported by C language.
Suppose variable A holds 10 and variable B holds 20 then:
Operator Description Example
+ Adds two operands.(numbers) A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiply both operands (numbes) A * B will give 200
/ Divide one operand by another operand B / A will give 2
% Modulus Operator finds remainder after an integer division B % A will give 0
CBPCC Page 1
Integer Arithmetic: -
When both the operands are integers, then the expression is known as an integer expression and the operation
is known as an integer arithmetic operation.
The result of the integer arithmetic is always the integer.
e.g. 5 + 6 = 11
34 – 3 = 31
3*3=9
4/2=2
9/5=1 (Fractional part is truncated)
8%5=3 (remainder value)
Real Arithmetic: -
If in arithmetic operation both the operands are real then the expression is known as the real expression and the
operation is known as the real arithmetic operation.
In real arithmetic result is always the real.
e.g. 4.5 + 43.3 = 47.8
5.5 - 1.2 = 4.3
We cannot use modulus operator with the real arithmetic.
Mixed mode arithmetic: -
If in arithmetic operation one operand is integer and another is real then this expression is known as the mixed
mode expression and operation is known as the mixed mode arithmetic operation.
e.g. 4.5 + 45 = 49.5
In mixed mode arithmetic result is real.
2. Relational Operators: -
The relational operators can do the comparison of two quantities(numbers).
The expression containing the relational operator is known as the relational expression.
C supports six different relational operators:
Operator Meaning
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
!= Not equal to
CBPCC Page 2
e.g. a < b is relational expression
10==20 is false
10!=50 is true
10 < 20 is true
20 < 10 is false
Note : The result of the relational operation is either true (1) or false (0).
The relational expression is used to check condition in if …. else statement in C.
3. Logical Operators: -
C provides following logical operators:
Operator Meaning
&& And
|| Or
! Not
These operators are used when we want to check more than one condition.
e.g. a > b && a == 10
10>20 && 10>30
a>b && a>c
a>10 || a>b
Note:
An expression, which contains logical operators, is known as the logical expression.
The result of the logical expression is also either true or false.
The logical expression is used to check multiple conditions in if …. else statement in C.
4. Bitwise Operators
C provides bitwise operators for manipulation (Calculation) of data at bit level.
Bitwise operators work on bits and perform bit by bit operation.
These operators are used for testing the bits, or shifting them right or left.
These operators can’t be used with the float or double.
0 0 0 0 0 1
0 1 0 1 1 1
1 1 1 1 0 0
1 0 0 1 1 0
CBPCC Page 4
The advantages of shorthand assignment operators are:
It is easy to write since we do not have to write the operand two times.
It is easy to read.
It is more efficient.
Unary Operator
When first one is used in assignment it first performs the assignment and then increment the value of ‘i’, while
later one is used then it increments the value of ‘i’ first and then performs assignment.
e.g. i=10;
a=i++;
printf(“i = %d \t a= %d\n”,i,a);
This statement print the output
i = 11 a = 10
i=10;
a= ++i;
printf(“i = %d \t a = %d\n”,i,a);
CBPCC Page 5
Syntax:
variable = Expression1 ? Expression2 : Expression3;
Working:
Here, Expression1 is the condition to be evaluated. If the condition(Expression1) is True then
Expression2 will be executed. Otherwise, if the condition(Expression1) is false then Expression3 will be
executed.
Example:
#include <stdio.h>
void main()
{
int a = 10, b = 20, c;
c=(a < b)?a:b;
printf("Minimum number=%d", c);
}
Output:
Minimum number=10
CBPCC Page 6
Comma Operator
Comma operator is used to link the expression together and entire expression is evaluated from left to right and
the right-most expression value is assigned to left side of the assignment operator.
e.g. v = (x= 10, y = 5, x + y);
In above expression first x has assigned value 10, then y has assigned value 5, and then addition operation of x
and y is performed and the result is assigned to the v.
sizeof Operator
It is operator, which gives the bytes occupied by the operand in memory. Operand may be constant, variable or
any other qualifier.
E.g. m = sizeof(int); // Give the size of integer data type
k = sizeof(14); // Give the no. of bytes occupied by simple value 14
l = sizeof(p); // Give the no. of bytes occupied by variable p
CBPCC Page 7