×
>
<

Aptitude

Operators In C

What is Operator ?

The symbols, however, are used to perform any type of mathematical and logical operation. In C programming, they are called Operators. In the programming, operators are used to manipulate data and variables.

The combination of operands and operators is called expression. Operands are variables that perform some operations in conjunction with the operators. 

Operator Types

Operators are of two types:

  • Unary operators
  • Binary operators

Unary operator:- These types of operators are used only with one operand.

Binary operator:-These types of operators are used with two operands.

Arithmetic operator

The arithmetic operators is used to perform mathematical operations like addition, subtraction division and multiplication etc. Below is a list for the type and function of these operators. Assume A=5 and B=10

OperatorFunction performed by operatorExample
+Adds two operands(float, integer etc.)A + B = 15
Subtracts  2nd operand from the 1st one.A − B = -5
*Multiplies the operands.A * B = 50
/Divides numerator by denominator.B / A = 2
%Returns remainder after dividing 1st operand with second one.B % A = 0
++Increment operator increases the integer value by one.++A = 6
Decrement operator decreases the integer value by one.A– = 4
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

OperatorFunction performed by operatorExample
=Simple assignment operator. Assigns values from right side operands to left side operandC = 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