In this chapter will understand types of Operators used in C.
Operators are used to perform mathematical and logical operations. For example, +, -, /, *, % etc are Operators.
+, -, *, /, %, ++ and -- are the arithmetic operators. For example let’s say we have two variables X and Y with values 10 and 2 respectively, thenArithmetic operator
/* X = 10 and Y = 2 */ X + Y = 12 (addition) X - Y = 8(subtraction) X * Y = 20(multiplication) X / Y = 5(Division) X % Y = 0(Modulas gives remainder) X++ = 11(increment variable by 1) Y++ = 3(called as increment operator) X-- = 9(decrement variable by 1) Y-- = 1(called as decrement operator)
In C, &&(AND), ||(OR) and !(NOT) Are the logical operators which returns either true or false. The ! Operator have only one operands. If we have two variables X and Y such that x = 1(true) and y = 0(false) then
X && Y Will return false X || Y Will return true !X will return false and !y will return true
&& Operator will return true only if both the operands are true, which in our case x was true but y was false.
|| Operator will return true when either of the two operands are true, in other words when both the operands are false, || will return false. Hence X || Y is true
The ! operator results the opposite of the variable, so when X is true !X will be false and when X is false !X will be true.
Assignment operator are used to assign values to a variable. For example,
x = 100; Here 100 is the value assigned to variable x using
These operators works on bits. They perform bit by bit operations that’s why they are known as bitwise operators.
In the next chapter, We will about operators.