what do you mean by type conversion in C Language or why it is necessary?


Type Conversion :

As the name suggests, type conversion refers to conversion of data from one form to another. Type Conversion is one of the most important aspects of C programming, where programmers are prone to commit errors. Hence, care should be taken that the code written, obeys the principles of type conversion.

Evaluation of expressions in Mixed Mode Arithmetic :

When an expression consists of different types of variables, C - Compiler follows rules of type conversion.

Types (Expression) Effect on other variables Resultant Type
Unsigned integer + Other data types/td> The other variables gets converted into unsigned integers. Unsigned integer
Long + Unsigned integer Unsigned integers changed to unsigned long. Unsigned long
Long + Any other data type. The other data types are converted into long. Long
Unsigned long + Other data type. The other data types gets converted into long. Double

For Example, consider two integer variables a = 2, b = 3; in general terms one would expect that the solution obtained by performing b / a would yield 1.5. But, this is not the case, when the same logic is applied in the programs. This is because, as both the variables a and b are declared integers, hence C compilers, considers the result of b / a as integer, eventually discarding the decimal value. Hence, proper type conversion is necessary to ensure accurate results.

The following are few basic rules which are to be followed while considering type conversion.

  • Unsigned char and unsigned short cab be converted into unsigned integer.
  • Character and short can be converted in to integer.
  • Float is converted into double.
  • int can be converted in to float.

Type Conversion is necessary because of the following reasons.

  1. it leads to accurate results and
  2. The code which is properly type converted prevents occurrence of large number of errors.
0 Komentar untuk "what do you mean by type conversion in C Language or why it is necessary?"

Back To Top