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.
7 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 [â€¦]
[â€¦] Division Operator in C [â€¦]
[â€¦] Division Operator in C [â€¦]
[â€¦] a recursive call to sumOfDigits function, pass the number n by removing the last digit using the division operator ( [â€¦]
[â€¦] We use the modulus operator ( number%10) to get the last digit of the number and then call the reverseNumber function recursively and pass the number by removing the last digit ( We can remove the last digit using the number/10 division operation) [â€¦]