Operators in C
Operators in C
C operators are one of the features in C which has symbols that can be used to
perform mathematical, relational, bitwise, conditional, or logical manipulations.
The C programming language has a lot of built-in operators to perform various
tasks as per the need of the program. Usually, operators take part in a program
for manipulating data and variables and form a part of the mathematical,
conditional, or logical expressions.
In other words, we can also say that an operator is a symbol that tells the
compiler to perform specific mathematical, conditional, or logical functions. It
is a symbol that operates on a value or a variable. For example, + and - are the
operators to perform addition and subtraction in any C program. C has many
operators that almost perform all types of operations. These operators are really
useful and can be used to perform every operation.
Types of Operators in C
1. Arithmetic Operator
2. Increment/Decrement Operator
3. Assignment Operator
4. Logical Operator
5. Bitwise Operator
6. Misc Operator
Let's look at these operators in c in detail.
Arithmetic Operators are the operators which are used to perform mathematical
calculations like addition (+), subtraction (-), multiplication (*), division (/), and
modulus (%). It performs all the operations on numerical values (constants and
variables).
The following table provided below shows all the arithmetic operators
supported by the C language for performing arithmetic operators.
Output:
a+b = 12
a-b = 2
a*b = 35
a/b = 1
Remainder when a divided by b = 2
The operators shown in the program are +, -, and * that computes addition,
subtraction, and multiplication respectively. In normal calculation, 7/5 = 1.4.
However, the output is 1 in the above program. The reason behind this is that
both the variables a and b are integers. Hence, the output should also be an
integer. So, the compiler neglects the term after the decimal point and shows 2
instead of 2.25 as the output of the program.
Using modulo operator (%), you can compute the remainder of any integer.
When a=7 is divided by b=5, the remainder is 2. If we want the result of our
division operator in decimal values, then either one of the operands should be a
floating-point number.
If we use the operator as a pre-fix, it adds 1 to the operand, and the result is
assigned to the variable on the left. Whereas, when it is used as a post-fix, it
first assigns the value to the variable on the left i.e., it first returns the original
value, and then the operand is incremented by 1.
Operator Description
Output:
++a = 12
--b = 89
++c = 101.500000
--d = 9.500000
In the above code example, the increment and decrement operators ++ and --
have been used as prefixes. Note that these two operators can also be used as
postfixes like a++ and a-- when required.
Assign
Output:
b=7
b = 14
b=7
b = 49
b=7
b=0
The table below shows all the relational operators supported by C. Here, we
assume that the variable A holds 15 and the variable B holds the 25.
Operator Description Example
Output:
8 == 10 is False(0)
8 != 10 is True(1)
8 > 10 is False(0)
8 < 10 is True(1)
8 >= 10 is False(0)
8 <=10 is True(1)
All the relational operators work in the same manner as described in the table
above.
3 simple steps to get noticed by recruiters from Top companies for your C
programming skills:
Step 1: Enroll in ‘C Basics Online Tutorial Course for Beginners’ for FREE
Step 3: Post completion, Unlock the verified certificate and share on your
resume/CV/ job profile
The table below shows all the logical operators supported by the C
programming language. We are here assuming that the variable A holds 7 and
variable B holds 3.
Following is the example that easily elaborates the working of the relational
operator:
Output:
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
Bitwise operators are the operators which work on bits and perform the bit-by-
bit operation. Mathematical operations like addition, subtraction, multiplication,
division, etc are converted to bit-level which makes processing faster and easier
to implement during computation and compiling of the program.
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
A = 00110010
B = 00011001
-----------------
A&B = 00010000
A|B = 00111011
A^B = 00101011
~A = 11001101
Binary OR Operator.
Besides all the other operators discussed above, the C programming language
also offers a few other important operators including sizeof, comma, pointer(*),
and conditional operator (?:).
Operator Precedence in C
Example: