×
>
<

Aptitude

Operators In C

What is Operator ?

Operator in Java is a symbol that is used to perform operations. For example: +, -, *, / etc.

There are many types of operators in Java which are given below:

  • Unary Operator,
  • Arithmetic Operator,
  • Shift Operator,
  • Relational Operator,
  • Bitwise Operator,
  • Logical Operator,
  • Ternary Operator and
  • Assignment Operator.

Operator Classifiation :
Operator TypeCategoryPrecedence
Unarypostfixexpr++ expr--
prefix++expr --expr +expr -expr ~ !
Arithmeticmultiplicative* / %
additive+ -
Shiftshift<< >> >>>
Relationalcomparison< > <= >= instanceof
equality== !=
Bitwisebitwise AND&
bitwise exclusive OR^
bitwise inclusive OR|
Logicallogical AND&&
logical OR||
Ternaryternary? :
Assignmentassignment= += -= *= /= %= &= ^= |= <<= >>= >>>=
Logical Oprator

The logic gate can be applied (finds application in mathematical operations too) with these operator. Logical operators are used with decision making statements. These operators are used to check two condition together in control statements The table about logical operators is being given. Assume A=5, B=10.

OperatorFunction performed by operatorExample
&&Logical AND operator. If both the conditions are satisfied then the condition becomes true .(A>8 && B==10) is false.
||Logical OR Operator. If any of the conditions are satisfied, then the condition becomes true.(A>8 || B==10) is true.
!Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.!(A>8 || B==10) is false.
Bitwise operator :

The bitwise operators work on bits of a integer value. Bitwise operators are used to perform bit level operations on given variables. The decimal values of the variables are converted to bits. After this the operations are performed on those bits, to understand it lets recall some basics –

pqp & qp | q
0000
0101
1111
1001

Assume A = 50 and B = 10 in binary, they can be written as: A = 0011 0010 B = 0000 1010 Now in the case of A and B we compare 2 bits one from A and the corresponding bit of B And bitwise comparison A&B= 0000 0010 Or bitwise comparison A|B=0011 1010 Assume A=50, B=10

OperatorFunction performed by operatorExample
&Binary AND Operator copies a bit to the result if it exists in both operands else copies zero.(A & B) = 2
|Binary OR Operator copies a bit if it exists in either operand.(A | B) = 58
^Bitwise exclusive OR Operator. If the corresponding bit of any of the operand is 1 then the output would be 1, otherwise 0.(A ^ B) = 56
~Binary compliment operator i.e.- 0 changes to 1 and 1 to 0(~A ) = -51
<<Binary Left shift Operator which rotates the number of bits to the left by specified positions as mentioned on the right.A << 2 = 200
>>Binary Right Rotation Operator which rotates the number of bits to right by specified positions as mentioned on the right.A >> 2 = 12
Relational operator :

Relational operators are used to compare values of two variables. As you can use these operators, you can find out, whether the values of any two variables are equal or not. If not equal, then the value of which variable is big and which variable’s value is small. Such operators are used with conditional statements. These operators are used to check the condition. when the condition is true, the value becomes true and the values becomes false when the condition is false. All the relational operators that are being used in C language are given below. Assume A=5, B=10

OperatorFunction performed by operatorExample
==Compares if the values of two operands are equal or not. If yes, then the condition becomes true, false otherwise.(A == B) is not true.
!=Compares if the values of two operands are equal or not. If the values are not equal, then the condition becomes true, false otherwise.(A != B) is true.
>Compares if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true, false otherwise.(A > B) is not true.
<Compares if the value of left operand is less than the value of right operand. If yes, then the condition becomes true, false otherwise.(A < B) is true.
>=Compares if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true, false otherwise.(A >= B) is not true.
<=Compares if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true, false otherwise.(A <= B) is true.
Assignment operator :

Assignment operators are used to assign values of variables to each other. The operator which changes the value of the operands themselves, here is how they work

Operator Function performed by operator Example
= Simple assignment operator. Assigns values from right side operands to left side operand C = A + B will assign the value of A + B to C
+= Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. C += A is equivalent to C = C + A
-= Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. C -= A is equivalent to C = C – A
*= Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand.C *= A is equivalent to C = C * A
/= Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand. C /= A is equivalent to C = C / A
%= Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. C %= A is equivalent to C = C % A
<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2
^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2
|= Bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2
Misc operator :

There are a few more miscellaneous operator worth mentioning

OperatorFunction performed by operatorExample
sizeof()Returns the size of a variable.sizeof(a), for an integer value, it returns 4.
&Returns the address of a variable.&a; returns the address of the memory location of variable.
*Pointer to a variable.*a;
? :Conditional Expression.If Condition is true ? then value X : otherwise value Y