Division Operator behaviour based on Datatypes in C language
We are already now that operators are used to perform certain operations on Operands. Now we are going to discuss Division Operator behaviour. How division operator works based on the Operands datatypes.
The behaviour of Operators. :
Operators behaviour is Independent of variable type. Have a look at following example.
1 2 3 4 5 6 
void main() { float a; a = 5 / 2; printf("a = %f \n",a); } 
what is the value of a in above example
Output :
1 
a = 2.000000 
Program Explanation:
 In General the value of 5/2 is 2.5 But in the C programming it will depends on the Operand type and Variable types.
 Here variable a is float variable and it is storing the value of 5/2 but the result is still 2. Because C operators are not variable Dependents (here a ).
 In other words, the datatype of the Resulting variable(here a) will not effects operator’s operation. Here variable a have floating datatype that means it can capable of storing floating point values but output is still only 2. So Variable datatypes is not affecting the result.
 In the C Programming Language, Operator’s operation is always dependent on operands datatype. In the above example if we have at least one floating type operand the result will be 2.5.(here 5 and 2 are operands and both are integer values).
Let’s look at another example, Where we take the floating point Operand.
Division Operator with Floating Point Operands :
1 2 3 4 5 6 
void main() { float a; a = 5.0 / 2; printf("a = %f n",a); } 
Output:
1 
a = 2.500000 
Program Explanation:
 In the above example, The Numerator value is floating point data value i.e 5.0. So The result of the division operation 5.0 / 2 is 2.500000
 That means division operator’s behaviour always depends on the operands datatype, not on the resulting variable datatype(here a is resulting Variable).
 But, If we want to store Floating point data resulting Variable must have float datatype.
Let’s look at another example, Where the resulting variable a is going to be an Integer datatype variable. Let’s see what will be the result of our program.
Division Operator behaviour with Integer result variable:
1 2 3 4 5 6 
void main() { int a; a = 5.0 / 2; printf("a = %d \n",a); } 
Program Output:
1 
a = 2 
Program Explanation
 In the above example, The Division Operation 5.0 / 2 is giving the result as 2.5. But the resulting variable a is an Integer variable and capable of storing the Integer value Data only. So variable a will end up storing the value 2 only.
 So from the above example, It is clear that, The resulting variable datatype also plays a role to get the desired results.
Quick Notes:
Operator’s behaviour is always depends on the Operands values. Operator’s behaviour is Independent of Resulting Variables datatype.
Conclusion :
If the both operands are Integers then result will be Integer. Any one of the Operands is float then result will be Float value.
Here is a small table with different types of operands and operators Results.
Operand 1

Operand 2

Result

int

int

int

float

int

float

int

float

float

float

float

float

 In the above first example, Two operands are integers so result is Integer.
 In the above second example, One operand is float and other one is integer so result is Float Value.
3 Responses
[…] Division Operator behavior based on datatypes […]
[…] Division Operator behavior based on datatypes […]
[…] our previous article, We have learned about the Division Operator in C Programming. In today’s article, We will continue with arithmetic operators and discuss the Modulus […]