Operators are special symbols used for specific purposes. An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. The various types of operators are:
- Arithmetical operators
- Assignment operators
- Increment/Decrement Operators
- Logical operators
- Relational operator
- Conditional operators
- Bitwise Operators
- Special operators
- Arithmetical operators
Arithmetical operators are used to performs simple arithmetic operation. These operators can be used with both integral and floating-point data types except Modulus or remainder % operator which is used only with the integral data type.
Arithmetic operators come under binary operators. Operators that have two operands are called binary operators.
|+||Adds two operands|
|–||Subtracts second operand from the first|
|*||Multiply both operands|
|/||Divide numerator by de-numerator|
|%||Modulus Operator and remainder of after an integer division|
2. Assignment operator
As the name suggests, the assignment operator ‘=’ is used for assigning a variable to a value. This operator takes the expression on its right-hand-side and places it into the variable on its left-hand-side.
For example a=5;
Compound Assignment Operators
|+ =||X + = Y||X = X + Y|
|– =||X – = Y||X = X – Y|
|% =||X % = Y||X = X % Y|
|/=||X / = Y||X = X / Y|
|*=||X * = Y||X = X * Y|
3. Increment and Decrement Operators
These operators are also called unary operators. Increment operator ‘++’ and Decrement operator ‘–‘ are used for incrementing and decrementing the value of a variable by 1. The increment/decrement operator can be used with any type of variable but it cannot be used with any constant. Increment and decrement operators each have two forms, pre and post.
Prefix operators: In Prefix form first variable is first incremented/decremented, then evaluated.
- Prefix Increment
- Prefix decrement
Postfix operators: In Postfix form first variable is first evaluated, then incremented/decremented.
- Postfix increment
- Postfix decrement
Example with increment operation :
x = ++a; //add one to a, store the result back in x
y= b++; //store the value of b to y then add one to b
x will be 6
y will be 10
a will be 6
b will be 11
Also Read : C Operators tricky question
4. Logical Operators:
The logical operators are used to combine one or more relational expression. The logical operators are
|&&||Called Logical AND operator. If both the operands are non zero then condition becomes true.|
|||||Called Logical OR Operator. If any of the two operands is non zero then condition becomes true.|
|!||Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.|
5. Relational operators
The relational operators are used to test the relation between two values. All relational operators are binary operators and therefore require two operands. A relational expression returns zero when the relation is false and a non-zero when it is true. The following table shows the relational operators.
|<=||Less than or equal to|
|>=||Greater than or equal to|
|! =||Not equal to|
6. Conditional operator
The conditional operator ?: is called ternary operator as it requires three operands. The format of the conditional operator is:
Conditional_ expression ? expression1 : expression2;
If the value of conditional expression is true then the expression1 is evaluated, otherwise expression2 is evaluated.int a = 1, b = 2;
x = (a > b) ? a : b;The condition evaluates to false, therefore x will take the value of b. Hence x=2
7. Bitwise Operators:
Bitwise operator works on bits and perform bit by bit operation.
|&||Binary AND Operator||It copies a bit to the result if it exists in both operands.|
||||Binary OR Operator||It copies a bit if it exists in either operand.|
|^||Binary XOR Operator||It copies the bit if it is set in one operand but not both.|
|~||Binary Ones Complement Operator||It is unary and has the effect of ‘flipping’ bits.|
|<<||Binary Left Shift Operator.||The left operands value is moved left by the number of bits specified by the right operand.|
|>>||Binary Right Shift Operator.||The left operands value is moved right by the number of bits specified by the right operand.|
8. Special Operator
- The comma operator
The comma operator gives left to right evaluation of expressions. When the set of expressions has to be evaluated for a value, only the rightmost expression is considered.
int a=1, b=2, c=3, i;
- The sizeof operator
The sizeof operator can be used to find how many bytes are required for an object to store in memory. For example
sizeof (char) gives 1
sizeof (int) gives 2
sizeof (float) gives 4