Operators in C
Operators in C
An operator in C can be defined as the symbol that helps us to perform some specific
mathematical, relational, bitwise, conditional, or logical computations on values and
variables. The values and variables used with operators are called operands. So we can say
that the operators are the symbols that perform operations on operands.
For example,
c = a + b;
Here, ‘+’ is the operator known as the addition operator, and ‘a’ and ‘b’ are operands. The
addition operator tells the compiler to add both of the operands ‘a’ and ‘b’.
Types of Operators in C
C language provides a wide range of operators that can be classified into 6 types based on
their functionality:
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
1. Arithmetic Operations in C
The arithmetic operators are used to perform arithmetic/mathematical operations on
operands. There are 9 arithmetic operators in C language:
S. No. Symbol Operator Description Syntax
Adds two
+ Plus a+b
1 numeric values.
Subtracts right
– Minus operand from a–b
2 left operand.
Multiply two
* Multiply a*b
3 numeric values.
Divide two
/ Divide a/b
4 numeric values.
Returns the
remainder after
% Modulus diving the left a%b
operand with the
5 right operand.
Used to specify
+ Unary Plus the positive +a
6 values.
Increases the
++ Increment value of the a++
8 operand by 1.
Decreases the
— Decrement value of the a–
9 operand by 1.
int main()
{
int a = 25, b = 5;
// using operators and printing results
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a % b = %d\n", a % b);
printf("+a = %d\n", +a);
printf("-a = %d\n", -a);
printf("a++ = %d\n", a++);
printf("a-- = %d\n", a--);
return 0;
}
Output
a + b = 30
a - b = 20
a * b = 125
a/b=5
a%b=0
+a = 25
-a = -25
a++ = 25
a-- = 26
2. Relational Operators in C
The relational operators in C are used for the comparison of the two operands. All these
operators are binary operators that return true or false values as the result of comparison.
These are a total of 6 relational operators in C:
S. No. Symbol Operator Description Syntax
Returns true if
the left operand
< Less than is less than the a<b
right operand.
1 Else false
Returns true if
the left operand
is greater than
> Greater than a>b
the right
operand. Else
2 false
S. No. Symbol Operator Description Syntax
Returns true if
the left operand
Less than or is less than or
<= a <= b
equal to equal to the right
operand. Else
3 false
Returns true if
the left operand
Greater than is greater than or
>= a >= b
or equal to equal to right
operand. Else
4 false
Returns true if
both the
== Equal to a == b
operands are
5 equal.
Returns true if
both the
!= Not equal to a != b
operands are
6 NOT equal.
3. Logical Operator in C
Logical Operators are used to combine two or more conditions/constraints or to complement
the evaluation of the original condition in consideration. The result of the operation of a
logical operator is a Boolean value either true or false.
S. No. Symbol Operator Description Syntax
Returns true if
both the
&& Logical AND a && b
operands are
1 true.
Returns true if
both or any of
|| Logical OR a || b
the operand is
2 true.
Returns true if
! Logical NOT the operand is !a
3 false.
int a = 25, b = 5;
printf("a && b : %d\n", a && b);
printf("a || b : %d\n", a || b);
printf("!a: %d\n", !a);
return 0;
}
Output
a && b : 1
a || b : 1
!a: 0
4. Bitwise Operators in C
The Bitwise operators are used to perform bit-level operations on the operands. The operators
are first converted to bit-level and then the calculation is performed on the operands.
Mathematical operations such as addition, subtraction, multiplication, etc. can be performed
at the bit level for faster processing.
There are 6 bitwise operators in C:
S. No. Symbol Operator Description Syntax
Performs bit-by-
bit AND
& Bitwise AND operation and a&b
returns the
1 result.
Performs bit-by-
bit OR operation
| Bitwise OR a|b
and returns the
2 result.
Performs bit-by-
bit XOR
^ Bitwise XOR operation and a^b
returns the
3 result.
Shifts the
number in
binary form by
Bitwise
<< one place in the a << b
Leftshift
operation and
returns the
5 result.
Shifts the
number in
binary form by
Bitwise
>> one place in the a >> b
Rightshilft
operation and
returns the
6 result.
int main()
{
int a = 25, b = 5;
return 0;
}
Output
a & b: 1
a | b: 29
a ^ b: 28
~a: -26
a >> b: 0
a << b: 800
5. Assignment Operators in C
Assignment operators are used to assign value to a variable. The left side operand of the
assignment operator is a variable and the right side operand of the assignment operator is a
value. The value on the right side must be of the same data type as the variable on the left
side otherwise the compiler will raise an error.
The assignment operators can be combined with some other operators in C to provide
multiple operations using single operator. These operators are called compound operators.
In C, there are 11 assignment operators :
S. No. Symbol Operator Description Syntax
Subtract the
right operand
Minus and and left operand
-= a -= b
assign and assign this
value to the left
3 operand.
Multiply the
right operand
Multiply and and left operand
*= a *= b
assign and assign this
value to the left
4 operand.
Assign the
remainder in the
Modulus and division of left
%= a %= b
assign operand with the
right operand to
6 the left operand.
Performs bitwise
AND and
AND and
&= assigns this a &= b
assign
value to the left
7 operand.
left operand.
Performs bitwise
XOR and
XOR and
^= assigns this a ^= b
assign
value to the left
9 operand.
Performs bitwise
Rightshift and
Rightshift and
>>= assign this value a >>= b
assign
to the left
10 operand.
Performs bitwise
Leftshift and
Leftshift and
<<= assign this value a <<= b
assign
to the left
11 operand.