Division Operator behaviour based on Datatypes in C language

Division-Operator-behaviour-based-on-datatype
 

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.

what is the value of a in above example

Output :

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 :

Output:

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:

Program Output:

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.
 
Also See :

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

7 Responses

  1. […] Division Operator behavior based on datatypes  […]

  2. […] Division Operator behavior based on datatypes  […]

  3. […] 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 […]

  4. […] Division Operator in C […]

  5. […] Division Operator in C […]

  6. […] a recursive call to sumOfDigits function, pass the number n by removing the last digit using the division operator ( […]

  7. […] 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) […]

Leave a Reply