Division operator behavior based on datatypes in C language

Spread the love
Operators in C language in depth Concepts

We are already now that operators are used to performing certain operations on Operands.now we are going to discuss Behavior of operators. sounds very strange yes we are going to learn Behavior of Operators.

The behavior of Operators. :

Operators behavior is Independent of variable type. confused have a look on this example.

Example :

  1. void main()
  2. {
  3.      float a;
  4.      a = 5 / 2;
  5.      printf(“a = %f n,a);
  6. }

what is the value of a in above example
Output :
a = 2.000000  
Explanation :

  • In normal mathematics 5/2 is 2.5 as all we know that but coming to c programming it will not be same as general mathematical Calculation.
  • Coming to our Example variable a is float variable and it is storing the value of 5/2 but the result is still 2 why?? Because C operators are not variable Dependents (here a ).
  • In other words, the datatype of the Resulting variable(a) will not effects operator’s operation. here variable a have floating datatype that means it can capable of storing decimal values but output is still only 2. so variable datatypes is not affecting the result.
  • Operator’s operation is always dependent on operands. 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).

Example 2 :

  1. void main()
  2. {
  3.      float a;
  4.      a = 5.0 / 2;
  5.      printf(“a = %f n,a);
  6. }

Output :
a = 2.500000 
Explanation :

  • From above example there is one operand have floating type data that is 5.0 and result of operator is changed we got result as 2.5 now.
  • That means operator’s behavior always depends on the operands datatype, not on the resulting variable(here a is resulting Variable).
  • Anyway if we want to store decimal data resulting Variable must have float datatype.
  • if we have the variable a as Integer type. the result going to be very interesting. Let us look on example of that type.

Example 3 :

  1. void main()
  2. {
  3.      int a;
  4.      a = 5.0 / 2;
  5.      printf(“a = %d n,a);
  6. }

Output :

  • From above example even operator is giving output as 2.5 but variable a is not capable to store Floating type value because it is integer Variable so Resulting Variable Datatype also plays a very good role.
  • But if our Resulting Variable is not capable of storing floating type data how can we expect floating type value as Output.. so operator’s Behavior always depends on the operands only.

Quick Notes:

Operator’s behavior is always depends on the Operands values.Operator’s behavior is Independent of Resulting Variables datatype.

Conclusion :

When we are using Binary operators. if the both operands are Integer then result will be integer.any one the Operand 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 our first example two operands are integers so result is Integer.
In our second example one operand is float and other one is integer so result is Float Value.

Also See :

Venkatesh Macha

Hi Guys, I am Venkatesh Macha. I am a programmer and an Open Source enthusiast, Presently working as a Software Engineer. Apart from that, I love to explore new technologies and things.

You may also like...

1 Response

Leave a Reply