## Type Conversion in C

The process of converting one data type into another data type is known as type conversion.

The automatic conversion of one data type into another data type is known as

__implicit type conversion__. It is done by the compiler.
The type conversion can also be done manually. The type conversion done by the programmer is known as

__explicit type conversion__.
We will discuss about these two types in detail in our next tutorials.

It is quite common that we have to use both integer and float numbers. While doing some arithmetic operations between them we should take care of few things which we will discuss in this tutorial.

1. Arithmetic operation between integer and integer will always result in an integer.

2. Arithmetic operation between float and float will always give float number.

3. Arithmetic operation between float and integer will always give float number. In this case, first integer will be promoted to float after that the arithmetic operation will take place.

### Type Conversion in Assignments

It is quite often that the variable on the left hand side of assignment operator (=) does not match the type of variable on its right hand side. In such a case, the value of the whole expression will be promoted or demoted on the basis of variable present on left hand side of assignment operator.

To make things a bit more clear lets take an example.

float a;

int b;

b=4.2;

a=3;

In the above example we are trying to store the float value (4.2) in integer variable b. So the value will be demoted to (4) and it will store in the integer variable. Same thing will happen with 3. As 3 is an integer, so it will be converted to float (3.00000) and then it will be stored in float variable.

Complexity of the arithmetic operation doesn’t matter. You should stick with the three basic rules. That’s it.

## Precedence (Hierarchy) of Operators in C

BODMAS is the basic rule to solve complex arithmetic problems in maths. Unfortunately BODMAS doesn’t work in C. Suppose you wrote one expression.

x*y+z*a

So how you will solve this expression?

This way (x*y)+(z*a)

Or this way x*(y+z)*a

To solve this problem in C you have to learn about hierarchy or precedence of operators. Hierarchy of some common arithmetic operators is given below.

#### Some Quick Tips

- Within nested parentheses, arithmetic operations will be done according to above priority. Expression under innermost parentheses will be solved first.
- It is advised to use parenthesis in the program carefully. You should type both parentheses first ( ). After that start typing expression inside it. This will remove the errors caused by missed parenthesis.

## Associativity of Operators in C

Associativity of operators is used when two operators of equal priority makes a tie. In that case the arithmetic operation is solved using the associativity table of C language.