5.0 C Operators
5.0 C Operators
•
In C•• language, operators are symbols that represent operations to be performed on one or
more operands. They are the basic components of the C programming. In this article, we will
learn about all the built-in operators in C with examples.
What is a C Operator?
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
6. Other 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 left a–b
2 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 a = 25, b = 5;
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 is
< Less than less than the right a<b
operand. Else
1 false
S. No. Symbol Operator Description Syntax
Returns true if
the left operand is
> Greater than greater than the a>b
right operand.
2 Else false
Returns true if
the left operand is
Less than or less than or equal
<= a <= b
equal to to the right
operand. Else
3 false
Returns true if
the left operand is
Greater than or greater than or
>= a >= b
equal to equal to right
operand. Else
4 false
Returns true if
== Equal to both the operands a == b
5 are equal.
Returns true if
!= Not equal to both the operands a != b
6 are NOT equal.
int main()
{
int a = 25, b = 5;
Output
a < b : 0
a > b : 1
a <= b: 0
a >= b: 1
a == b: 0
a != b : 1
Returns true if
&& Logical AND both the operands a && b
1 are true.
Returns true if
|| Logical OR both or any of the a || b
2 operand is true.
Returns true if
! Logical NOT the operand is !a
3 false.
int main()
{
int a = 25, b = 5;
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 a&b
operation and
1 returns the result.
Performs bit-by-
bit OR operation
| Bitwise OR a|b
and returns the
2 result.
Performs bit-by-
bit XOR
^ Bitwise XOR a^b
operation and
3 returns the result.
operation and
returns the 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
Assign the
Modulus and remainder in the
%= a %= b
assign division of left
6 operand with the
S. No. Symbol Operator Description Syntax
right operand to
the left operand.
Performs bitwise
AND and assigns
&= AND and assign a &= b
this value to the
7 left operand.
Performs bitwise
OR and assigns
|= OR and assign a |= b
this value to the
8 left operand.
Performs bitwise
XOR and assigns
^= XOR and assign a ^= b
this value to the
9 left 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.
int main()
{
int a = 25, b = 5;
return 0;
}
Output
a = b: 5
a += b: 10
a -= b: 5
a *= b: 25
a /= b: 5
a %= b: 0
a &= b: 0
a |= b: 5
a >>= b: 0
a <<= b: 0
6. Other Operators
Apart from the above operators, there are some other operators available in C used to
perform some specific tasks. Some of them are discussed here:
sizeof Operator
• sizeof is much used in the C programming language.
• It is a compile-time unary operator which can be used to compute the size of its
operand.
• The result of sizeof is of the unsigned integral type which is usually denoted by
size_t.
• Basically, the sizeof the operator is used to compute the size of the variable or
datatype.
Syntax
sizeof (operand)
and
structure_pointer -> member;
To know more about dot operators refer to this article and to know more about arrow(->)
operators refer to this article.
Cast Operator
• Casting operators convert one data type to another. For example, int(2.2000) would
return 2.
• A cast is a special operator that forces one data type to be converted into another.
• The most general cast supported by most of the C compilers is as follows − [ (type)
expression ].
Syntax
(new_type) operand;
int main()
{
// integer variable
int num = 10;
int* add_of_num = #
return 0;
}
Output
sizeof(num) = 4 bytes
&num = 0x7ffe2b7bdf8c
*add_of_num = 10
(10 < 5) ? 10 : 20 = 20
(float)num = 10.000000
Postfix increment/decrement (a
a++ , a– left-to-right
1 is a variable)
Prefix increment/decrement (a
++a , –a right-to-left
is a variable)
Logical negation/bitwise
!,~ right-to-left
complement
* Dereference right-to-left
10 | Bitwise OR left-to-right
12 || Logical OR left-to-right
= Assignment right-to-left
Addition/subtraction
+= , -= right-to-left
assignment
Multiplication/division
*= , /= right-to-left
assignment
Modulus/bitwise AND
%= , &= right-to-left
assignment
Bitwise exclusive/inclusive OR
^= , |= right-to-left
14 assignment
Precedence Operator Description Associativity
Conclusion
The points we learned about the operator are as follows:
• Operators are symbols used for performing some kind of operation in C.
• There are six types of operators, Arithmetic Operators, Relational Operators, Logical
Operators, Bitwise Operators, Assignment Operators, and Miscellaneous Operators.
• Operators can also be of type unary, binary, and ternary according to the number of
operators they are using.
• Every operator returns a numerical value except logical, relational, and conditional
operator which returns a boolean value (true or false).
• There is a Precedence in the operators means the priority of using one operator is
greater than another operator.